Results 1 to 2 of 2

Thread: How can I read DataTable rows one by into a DatagridView

  1. #1
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    10

    How can I read DataTable rows one by into a DatagridView

    Instead of assigning Datagridview.datasource =MyDataTable , I want to read DataTable rows one by one into Datagridview (Due to some reasons). I tried as under but it shows the last row only.

    can any body help pls?


    Code:
     Private Sub BtnShowBills_Click(sender As System.Object, e As System.EventArgs) Handles BtnShowBills.Click
            GetConnect()
            Using cmd As New SqlCommand("USP_ShowVendorDetailBill", Conn)
                cmd.CommandType = CommandType.StoredProcedure
                Conn.Open()
                cmd.Parameters.AddWithValue("@PurBillNo", TextBox1.Text)
              
      Using reader As SqlDataReader = cmd.ExecuteReader
                    Dim dt As New DataTable
                    Dim i As Integer = 0
                    
                    dt.Load(reader)
                    For i = 0 To dt.Rows.Count - 1
                        PurchaseGrid.Rows.Item(0).Cells(0).Value = dt.Rows(i)("ProductID")
                        PurchaseGrid.Rows.Item(0).Cells(1).Value = dt.Rows(i)("PurDescription") 
                        PurchaseGrid.Rows.Item(0).Cells(2).Value = dt.Rows(i)("PurQty")
                        PurchaseGrid.Rows.Item(0).Cells(3).Value = dt.Rows(i)("PurRate") 
                        PurchaseGrid.Rows.Item(0).Cells(4).Value = dt.Rows(i)("Amount")  
    
                    Next
    End Using
            End Using
        End Sub

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,807
    Reputation
    1705
    Why would you want to do that? There is no good reason to do so. You should absolutely populate a DataTable and assign it to the DataSource of the grid unless you have a compelling reason to do otherwise.

    Assuming that you do have such a reason, don't use a DataTable at all. It's silly to call Load to populate a DataTable from a data reader and then loop through the Rows of the DataTable when you could simply loop through the data reader directly.

    As for your issue, the problem is this:
    Code:
                    For i = 0 To dt.Rows.Count - 1
                        PurchaseGrid.Rows.Item(0).Cells(0).Value = dt.Rows(i)("ProductID")
                        PurchaseGrid.Rows.Item(0).Cells(1).Value = dt.Rows(i)("PurDescription") 
                        PurchaseGrid.Rows.Item(0).Cells(2).Value = dt.Rows(i)("PurQty")
                        PurchaseGrid.Rows.Item(0).Cells(3).Value = dt.Rows(i)("PurRate") 
                        PurchaseGrid.Rows.Item(0).Cells(4).Value = dt.Rows(i)("Amount")  
    
                    Next
    Instead of adding a new row to the grid for each row in the table, you just keep overwriting the values in the first row of the grid. In that case, of course you're only going to see the last record because it's the last one you write. If you want to see all the records then you have to write each of them to a new row in the grid. If you really want to avoid binding then your code should look something like this:
    While reader.Read()
    PurchaseGrid.Rows.Add(reader("ProductID"),
    reader("PurDescription"),
    reader("PurQty"),
    reader("PurRate"),
    reader("Amount"))
    End While

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •