Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. search items in Datagridview

search items in Datagridview

Scheduled Pinned Locked Moved Visual Basic
helpquestion
17 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Scubapro

    For v = 0 To DataGridView1.Rows.Count - 1
    id = DataGridView1.Rows(v).Cells(0).value
    'Do your thing'
    Next v

    Z Offline
    Z Offline
    zafax_
    wrote on last edited by
    #5

    i used ur code working same as mine, only first row cell it works not for rest ! what i am missing ? thanks for your reply any more ideas ?

    so much of happy ending...

    S D 2 Replies Last reply
    0
    • L Luc Pattyn

      Your foreach loop enumerates all rows, so you will be adding a parameter (called "@id") for each of them; I don't think that is appropriate, and from your symptom description it seems the first one sticks. Suggestion: either try using the "current row" rather than all rows, or search all rows for the (first) one that actually holds some data in cell 0. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

      Z Offline
      Z Offline
      zafax_
      wrote on last edited by
      #6

      Luc Pattyn well i been trying ur idea but if u show me by peace code will be great !! :) thanks

      so much of happy ending...

      1 Reply Last reply
      0
      • Z zafax_

        i used ur code working same as mine, only first row cell it works not for rest ! what i am missing ? thanks for your reply any more ideas ?

        so much of happy ending...

        S Offline
        S Offline
        Scubapro
        wrote on last edited by
        #7

        Describe exactly what you want to do after you've got the cells value in your 'id'-variable.

        Z 1 Reply Last reply
        0
        • Z zafax_

          i used ur code working same as mine, only first row cell it works not for rest ! what i am missing ? thanks for your reply any more ideas ?

          so much of happy ending...

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #8

          Its not clear what you are trying to do, but the code you are using will only iterate each row, if you are trying to also iterate each cell then do the following;

                  For Each row As DataGridViewRow In DataGridView.Rows
                      For Each cell As DataGridViewCell In row.Cells
                          'Do your necessary test etc against each cell here
                      Next
                  Next
          

          Dave Don't forget to rate messages!
          Find Me On: Web|Facebook|Twitter|LinkedIn
          Waving? dave.m.auld[at]googlewave.com

          1 Reply Last reply
          0
          • S Scubapro

            Describe exactly what you want to do after you've got the cells value in your 'id'-variable.

            Z Offline
            Z Offline
            zafax_
            wrote on last edited by
            #9

            @Scubapro i need to search data inside datagridview only , when user enter an id number in the id column in every row which should display its details . the code is working fine but not with other rows. i hope u understand this. thanks.

            so much of happy ending...

            R L 2 Replies Last reply
            0
            • Z zafax_

              @Scubapro i need to search data inside datagridview only , when user enter an id number in the id column in every row which should display its details . the code is working fine but not with other rows. i hope u understand this. thanks.

              so much of happy ending...

              R Offline
              R Offline
              riced
              wrote on last edited by
              #10

              zafax_ wrote:

              the code is working fine

              Then there is no problem? :-D

              Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

              1 Reply Last reply
              0
              • Z zafax_

                @Scubapro i need to search data inside datagridview only , when user enter an id number in the id column in every row which should display its details . the code is working fine but not with other rows. i hope u understand this. thanks.

                so much of happy ending...

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                You want similar behaviour on the second column? What's the name and datatype of the second column?

                I are Troll :suss:

                Z 1 Reply Last reply
                0
                • L Lost User

                  You want similar behaviour on the second column? What's the name and datatype of the second column?

                  I are Troll :suss:

                  Z Offline
                  Z Offline
                  zafax_
                  wrote on last edited by
                  #12

                  @Eddy Vluggen, see the following ... Column name is id. user enters id number on this column. for retrieving details . For Each row As Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows() Dim id as integer id = row.cell(0).value cmd.Parameters.Add("@id", SqlDbType.int).Value = id next row id | name | address |age 1 | abcd | abcd |abcd 2 | 123 | rr | fr it works only on first row cell. thanks

                  so much of happy ending...

                  L 1 Reply Last reply
                  0
                  • Z zafax_

                    @Eddy Vluggen, see the following ... Column name is id. user enters id number on this column. for retrieving details . For Each row As Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows() Dim id as integer id = row.cell(0).value cmd.Parameters.Add("@id", SqlDbType.int).Value = id next row id | name | address |age 1 | abcd | abcd |abcd 2 | 123 | rr | fr it works only on first row cell. thanks

                    so much of happy ending...

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    ..and you want the same functionality in the second row cell? Something like this?

                    For Each row As Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows()
                    Dim searchVal as String
                    searchVal = row.cell(1).value
                    cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = searchVal
                    next row

                    That would be flawed too, since you'd still be adding a lot of SqlParameter objects to the command. Can you post the entire method, including the parts where you create and execute the SqlCommand?

                    I are Troll :suss:

                    Z 1 Reply Last reply
                    0
                    • L Lost User

                      ..and you want the same functionality in the second row cell? Something like this?

                      For Each row As Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows()
                      Dim searchVal as String
                      searchVal = row.cell(1).value
                      cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = searchVal
                      next row

                      That would be flawed too, since you'd still be adding a lot of SqlParameter objects to the command. Can you post the entire method, including the parts where you create and execute the SqlCommand?

                      I are Troll :suss:

                      Z Offline
                      Z Offline
                      zafax_
                      wrote on last edited by
                      #14

                      items search through storedprocedure the entire code here... Private Sub getItems() c.ConS() c.Cm.Open() For Each row As DataGridViewRow In Me.ItemsDataGridView.Rows Dim cod As Integer cod = row.Cells(0).Value Dim cmd As New SqlCommand("getItems", c.Cm) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add("@cod", int).Value = cod Dim da As New SqlDataAdapter da.SelectCommand = cmd da.Fill(dat) Me.bind.DataSource = dat Me.ItemsDataGridView.DataSource = bind Next row End Sub

                      so much of happy ending...

                      L 1 Reply Last reply
                      0
                      • Z zafax_

                        items search through storedprocedure the entire code here... Private Sub getItems() c.ConS() c.Cm.Open() For Each row As DataGridViewRow In Me.ItemsDataGridView.Rows Dim cod As Integer cod = row.Cells(0).Value Dim cmd As New SqlCommand("getItems", c.Cm) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add("@cod", int).Value = cod Dim da As New SqlDataAdapter da.SelectCommand = cmd da.Fill(dat) Me.bind.DataSource = dat Me.ItemsDataGridView.DataSource = bind Next row End Sub

                        so much of happy ending...

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #15

                        If I understood correctly then you want to use this code to (also) filter on the "name" column? Can you post the source of the stored procedure "getItems"?

                        I are Troll :suss:

                        Z 1 Reply Last reply
                        0
                        • L Lost User

                          If I understood correctly then you want to use this code to (also) filter on the "name" column? Can you post the source of the stored procedure "getItems"?

                          I are Troll :suss:

                          Z Offline
                          Z Offline
                          zafax_
                          wrote on last edited by
                          #16

                          getitems()is the query which retrieves data i need to filter cod in id column . i dont know y u want see the code for stored procedure since its just a query ...... select id ,A,B,C from Items where Code =@cod :-D

                          so much of happy ending...

                          1 Reply Last reply
                          0
                          • Z zafax_

                            the followind code is used for search items from a column cell For Each row As Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows() Dim id as integer id = row.cell(0).value cmd.Parameters.Add("@id", SqlDbType.int).Value = id next row when i enter a value in the Column cell(0) on first row works fine but the problem is when i enter a value in second,third ... row it doesn't work as it should be. can anyone help me to find out what i am missing ? thanks !!!

                            so much of happy ending...

                            S Offline
                            S Offline
                            slam Iqbal
                            wrote on last edited by
                            #17

                            zafax_ Wrote: when i enter a value in the Column cell(0) on first row works fine but the problem is when i enter a value in second,third ... row it doesn't work as it should be. What is the error description?

                            Use <pre lang="vb"> Visual Basic Code Here.</pre>

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups