JasonWilliam
asked on
Tracking a row in a DataGridView regardless of sort?
Hey all. I'm trying to figure out how to track a row, regardless of how a user may sort my datagridview. In other words, lets say I have a sample table that looks like this:
# Name Notes
1 John Good guy
2 Jane Good girl
3 Dawn Bad girl
If I were do get the value of row 0, column "Name", I get "John" as I expect
Now, lets say the user sorts on the "#" column, so the table now looks like this:
# Name Notes
3 Dawn Bad girl
2 Jane Good girl
1 John Good guy
If I execute that same get statement, I now get "Dawn" instead of "John".
My question is, how do I always make sure I get "John"?
# Name Notes
1 John Good guy
2 Jane Good girl
3 Dawn Bad girl
If I were do get the value of row 0, column "Name", I get "John" as I expect
Now, lets say the user sorts on the "#" column, so the table now looks like this:
# Name Notes
3 Dawn Bad girl
2 Jane Good girl
1 John Good guy
If I execute that same get statement, I now get "Dawn" instead of "John".
My question is, how do I always make sure I get "John"?
Whoops, I forgot to increment the i value
Dim strChoice As String
Dim i As Integer = 0
For each objDR in objDS.Tables("STUFF").Rows
if objDS.Tables("STUFF").Rows (i).Item(" Name") = strChoice then
'/// do your bloack here ///
Exit For
end if
i += 1 '/// very important little integer ///
Next
Dim strChoice As String
Dim i As Integer = 0
For each objDR in objDS.Tables("STUFF").Rows
if objDS.Tables("STUFF").Rows
'/// do your bloack here ///
Exit For
end if
i += 1 '/// very important little integer ///
Next
ASKER
Thanks for the quick response!
I came up with something similar to this and it does work as advertised. However, this table in reality has 96 entries,12 columns and each column is updated 4 times a second. A loop like this is pretty prohibitive considering the frequency in which its going to be run.
Is there no built in provision or non-loop alternative?
I came up with something similar to this and it does work as advertised. However, this table in reality has 96 entries,12 columns and each column is updated 4 times a second. A loop like this is pretty prohibitive considering the frequency in which its going to be run.
Is there no built in provision or non-loop alternative?
if you see the values from the underlying datatable that will be in the original order. only dataview is effected when you sort the gridview.
to test do the following.
in a new project add a gridview control to the default form.
now add the following code to the form load event.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 5
AddColumn(i)
Next
DataGridView1.DataSource = GetdataSource()
End Sub
Private Sub AddColumn(ByVal colIndex As Integer)
Dim dataGridViewColumn As New DataGridViewTextBoxColumn
With dataGridViewColumn
.Name = "columnName" & colIndex.ToString
.HeaderText = "headerText" & colIndex.ToString
.DataPropertyName = "columnName" & colIndex.ToString
.Width = 40
.HeaderCell.Style.Alignmen t = DataGridViewContentAlignme nt.MiddleC enter
.ToolTipText = Nothing
End With
DataGridView1.Columns.Inse rt(0, dataGridViewColumn)
End Sub
Private Function GetdataSource() As DataTable
Dim retList As New DataTable
For i As Integer = 0 To 5
retList.Columns.Add("colum nName" & i)
Next
For i As Integer = 0 To 5
Dim dr As DataRow = retList.NewRow
For col As Integer = 0 To 5
dr.Item("columnName" & col) = " Row :" & i.ToString & " Col :" & col.ToString
Next
retList.Rows.Add(dr)
Next
Return retList
End Function
add two button controls to the form
and add the following code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rowCnt As Integer = CType(DataGridView1.DataSo urce, DataTable).Rows.Count
Dim colCnt As Integer = CType(DataGridView1.DataSo urce, DataTable).Columns.Count
Trace.WriteLine("Using Datatable")
For rowNum As Integer = 0 To rowCnt - 1
For colNum As Integer = 0 To colCnt - 1
Trace.Write(CType(DataGrid View1.Data Source, DataTable).Rows(rowNum).It em(colNum) .ToString)
Next
Trace.WriteLine("")
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowCnt As Integer = CType(DataGridView1.DataSo urce, DataTable).Rows.Count
Dim colCnt As Integer = CType(DataGridView1.DataSo urce, DataTable).Columns.Count
Trace.WriteLine("Using Dataview")
For rowNum As Integer = 0 To rowCnt - 1
For colNum As Integer = 0 To colCnt - 1
Trace.Write(CType(DataGrid View1.Data Source, DataTable).DefaultView.Ite m(rowNum). Item(colNu m).ToStrin g)
Next
Trace.WriteLine("")
Next
End Sub
now run the program and click on any column header to sort the gridview and click button1 and button2 to see the results.
to test do the following.
in a new project add a gridview control to the default form.
now add the following code to the form load event.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 5
AddColumn(i)
Next
DataGridView1.DataSource = GetdataSource()
End Sub
Private Sub AddColumn(ByVal colIndex As Integer)
Dim dataGridViewColumn As New DataGridViewTextBoxColumn
With dataGridViewColumn
.Name = "columnName" & colIndex.ToString
.HeaderText = "headerText" & colIndex.ToString
.DataPropertyName = "columnName" & colIndex.ToString
.Width = 40
.HeaderCell.Style.Alignmen
.ToolTipText = Nothing
End With
DataGridView1.Columns.Inse
End Sub
Private Function GetdataSource() As DataTable
Dim retList As New DataTable
For i As Integer = 0 To 5
retList.Columns.Add("colum
Next
For i As Integer = 0 To 5
Dim dr As DataRow = retList.NewRow
For col As Integer = 0 To 5
dr.Item("columnName" & col) = " Row :" & i.ToString & " Col :" & col.ToString
Next
retList.Rows.Add(dr)
Next
Return retList
End Function
add two button controls to the form
and add the following code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rowCnt As Integer = CType(DataGridView1.DataSo
Dim colCnt As Integer = CType(DataGridView1.DataSo
Trace.WriteLine("Using Datatable")
For rowNum As Integer = 0 To rowCnt - 1
For colNum As Integer = 0 To colCnt - 1
Trace.Write(CType(DataGrid
Next
Trace.WriteLine("")
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowCnt As Integer = CType(DataGridView1.DataSo
Dim colCnt As Integer = CType(DataGridView1.DataSo
Trace.WriteLine("Using Dataview")
For rowNum As Integer = 0 To rowCnt - 1
For colNum As Integer = 0 To colCnt - 1
Trace.Write(CType(DataGrid
Next
Trace.WriteLine("")
Next
End Sub
now run the program and click on any column header to sort the gridview and click button1 and button2 to see the results.
ASKER
Appari, thanks! I do see what that's doing. But, now add a third button and this code to the project:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Trace.Write(DataGridView1. Item(0, 0).Value)
End Sub
See what happens? 0,0 refers to two different cells, depending on the sort; its a relative addressing. I need to count on 0,0 being a global address.
That making any sense?
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Trace.Write(DataGridView1.
End Sub
See what happens? 0,0 refers to two different cells, depending on the sort; its a relative addressing. I need to count on 0,0 being a global address.
That making any sense?
what i am saying is instead of reading it from datagridview get the values from datatable bounded to your grid view. if you know the address the value is same in datatable irrespectvie of where the data is displayed on the gridview.
ASKER
Ok I think I follow you... but one last thing... Can you please edit the code above to show me how to update datasource cell 0,0 with a value, say "Test", on a press of a button, say Button 3? I can't figure out how; all I seem to be able to do is update datagridview cell 0,0 (which as we've shown is relative to the sort)..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome!! That is exactly the answer. Points, and my thanks, for you appari!
Dim strChoice As String
Dim i As Integer = 0
For each objDR in objDS.Tables("STUFF").Rows
if objDS.Tables("STUFF").Rows
'/// do your bloack here ///
Exit For
end if
Next
Would this get the type of thing you want??
John