|
-
Sep 27th, 2005, 03:30 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] A simple Data set / grid question
Morning,
I have a data grid bound to a data set. Depending on what row people click on in the data grid I look up data from the data set and populate other controls/variables. I do this using Datagrid1.CurrentRowIndex to match the corresponding data, if someone does a sort on the data grid will this prevent the row indexes between the grid and the data set from matching up?
-
Sep 27th, 2005, 03:43 AM
#2
Re: A simple Data set / grid question
-
Sep 27th, 2005, 03:44 AM
#3
Re: A simple Data set / grid question
Yes. When you bind a DataTable to a control, the data in the control actually corresponds to the data in the DefaultView of the DataTable. When you sort the DataGrid by clicking a column header, you are actually setting the Sort property of the bound table's DefaultView. All this means that you can still get the corresponding row by indexing the DefaultView property of the DataTable rather than the Rows property. Note that the object returned will thus be a DataRowView rather than a DataRow. You can still index it to get a field value, but it doesn't behave exactly the same way in all circumstances. It has a Row property that returns the actual DataRow from the original DataTable.
-
Sep 27th, 2005, 03:45 AM
#4
Thread Starter
Frenzied Member
Re: A simple Data set / grid question
 Originally Posted by mar_zim
Yes.
Thanks do you know a better way then of retrieving the correlating data between the two?
-
Sep 27th, 2005, 03:59 AM
#5
Re: A simple Data set / grid question
 Originally Posted by FishGuy
Thanks do you know a better way then of retrieving the correlating data between the two?
I've get this in this site: http://www.syncfusion.com/FAQ/Window...4c.aspx#q1145q
VB Code:
Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
Dim bm As BindingManagerBase = Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
Dim dr As DataRow = CType(bm.Current, DataRowView).Row
MessageBox.Show(dr(1).ToString())<---second column of the datatable
End Sub
-
Sep 27th, 2005, 04:05 AM
#6
Re: A simple Data set / grid question
The DataRowView you are retrieveing is the same one you would get by:
VB Code:
DirectCast(Me.DataGrid1.DataSource, DataTable).DefaultView(Me.DataGrid1.CurrentRowIndex)
Also, there is no need to then get the DataRow from the DataRowView because these two lines of code return exactly the same thing:
VB Code:
Dim val1 as Object = myDataRowView(1)
Dim val2 as Object = myDataRowView.Row(1)
-
Sep 27th, 2005, 04:24 AM
#7
Thread Starter
Frenzied Member
Re: A simple Data set / grid question
 Originally Posted by jmcilhinney
Yes. When you bind a DataTable to a control, the data in the control actually corresponds to the data in the DefaultView of the DataTable. When you sort the DataGrid by clicking a column header, you are actually setting the Sort property of the bound table's DefaultView. All this means that you can still get the corresponding row by indexing the DefaultView property of the DataTable rather than the Rows property. Note that the object returned will thus be a DataRowView rather than a DataRow. You can still index it to get a field value, but it doesn't behave exactly the same way in all circumstances. It has a Row property that returns the actual DataRow from the original DataTable.
Thanks, I think I understand what you are saying how is this done in comparison to waht I have at the moment.
VB Code:
Dim CurrentRow As Integer = Me.egrdSearchResult.CurrentRowIndex
Dim griddata = Me.egrdSearchResult(CurrentRow, 0)
Dim dsdata = objDataBaseClass.SqldsSearchCustomer.Tables(0).Rows(CurrentRow).Item(0)
MsgBox(griddata & " - " & dsdata)
On first running my sample it works(griddata and dsdata return same value) but reordering the grid causes the grdata to return the desired value but the dsdata has kept its order so returns the wrong value. Could you please show a line of code so I can see what you mean.
Sorry just got your replies
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|