Results 1 to 5 of 5

Thread: [RESOLVED] DataGridView with DataRow

  1. #1

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Resolved [RESOLVED] DataGridView with DataRow

    Hi all,

    I want to know how can I map the data inside datarow() into datagridview???

    Here is detail:

    I have a dataTable which contains data from DB.
    I use a search function to search this table.

    Code:
    Dim dRow() as DataRow = dtStudent.Select("sID='" & txtsID.Text.Trim & "'")
    If dRow.Length <> 0 Then
      ' This not working!!
      ' dgView.DataSource = dRow(0)
    
      ' This also not working!!
      ' dgView.DataSource = dRow(0).Table
    End If

    I can use a stored procedure and go back the DB loop again.
    But since I have all the data in my memory. I want to search this DataTable and then display it in grid instead of going back to server and get the data.

    Can anybody help me??

    Thanks.
    scsfdev
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

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

    Re: DataGridView with DataRow

    Bind the DataTable to a BindingSource, bind the BindingSource to the grid and then set the Filter property of BindingSource. Binding consists of simply assigning to the DataSource property and the Filter value is exactly what you're passing to the Select method now.
    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

  3. #3

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: DataGridView with DataRow

    Quote Originally Posted by jmcilhinney View Post
    Bind the DataTable to a BindingSource, bind the BindingSource to the grid and then set the Filter property of BindingSource. Binding consists of simply assigning to the DataSource property and the Filter value is exactly what you're passing to the Select method now.
    Hi jmcilhinney,

    thanks for your tips.
    This is my first time to play with BindingSource.
    I learnt new things thanks.

    And another problem is I already bind these 2 and now my grid are displaying OK (thanks for your helping ) but my grid allow user to Sort based on their desire column.
    And user can double click on a row and view that item in Detail View.
    And my detail form also has "Record 1/200" or "Record 3/200" (current record that user is view in detail mode.)

    I tried to think how can I get this 1 or 3 current record.
    If user sort the grid and double click to view detail, I can't use the row Index to retrieve data.

    The only way I can think of is loop the master datatable and get the index. But if i have more than 200 or 400 records, i think again on Processing time and memory usage.

    I also tried the bindingsource.Position (This also not working. it also based on already sorted index)

    any idea???


    Thanks.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

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

    Re: DataGridView with DataRow

    vb.net Code:
    1. Dim view As DataRowView = DirectCast(myBindingSource.Current, DataRowView)
    2. Dim row As DataRow = view.Row
    3. Dim rowIndex As Integer = myDataTable.Rows.IndexOf(row)
    That will give you the zero-based row index based on the data in the 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

  5. #5

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: DataGridView with DataRow

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. Dim view As DataRowView = DirectCast(myBindingSource.Current, DataRowView)
    2. Dim row As DataRow = view.Row
    3. Dim rowIndex As Integer = myDataTable.Rows.IndexOf(row)
    That will give you the zero-based row index based on the data in the DataTable.
    Hi jmcilhinney,

    You save me again
    Thanks.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

Tags for this Thread

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