search items in Datagridview
-
For v = 0 To DataGridView1.Rows.Count - 1
id = DataGridView1.Rows(v).Cells(0).value
'Do your thing'
Next v -
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.
-
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...
-
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...
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 -
Describe exactly what you want to do after you've got the cells value in your 'id'-variable.
@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...
-
@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...
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.
-
@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...
-
You want similar behaviour on the second column? What's the name and datatype of the second column?
I are Troll :suss:
@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...
-
@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...
..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 rowThat 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 theSqlCommand
?I are Troll :suss:
-
..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 rowThat 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 theSqlCommand
?I are Troll :suss:
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...
-
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...
-
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:
-
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...
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>