Results 1 to 7 of 7

Thread: [RESOLVED] A simple Data set / grid question

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [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?

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: A simple Data set / grid question

    Yes.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: A simple Data set / grid question

    Quote Originally Posted by mar_zim
    Yes.
    Thanks do you know a better way then of retrieving the correlating data between the two?

  5. #5
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: A simple Data set / grid question

    Quote 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:
    1. Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
    2.         Dim bm As BindingManagerBase = Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
    3.         Dim dr As DataRow = CType(bm.Current, DataRowView).Row
    4.         MessageBox.Show(dr(1).ToString())<---second column of the datatable
    5.     End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: A simple Data set / grid question

    The DataRowView you are retrieveing is the same one you would get by:
    VB Code:
    1. 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:
    1. Dim val1 as Object = myDataRowView(1)
    2. Dim val2 as Object = myDataRowView.Row(1)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: A simple Data set / grid question

    Quote 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:
    1. Dim CurrentRow As Integer = Me.egrdSearchResult.CurrentRowIndex
    2.         Dim griddata = Me.egrdSearchResult(CurrentRow, 0)
    3.         Dim dsdata = objDataBaseClass.SqldsSearchCustomer.Tables(0).Rows(CurrentRow).Item(0)
    4.         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
  •  



Click Here to Expand Forum to Full Width