I have come across one problem where my DataGridView is only displaying the last record. I was surprised to see this because there are 30 rows in the table which all are needed to display in DataGridView after querying.
After lots of research on the internet, I found that I am not the only victim of this situation.(:D).
Below is my code which I had used before.
- Dim sadapt As New OleDbDataAdapter("SELECT * FROM [OrderDetail],[Order] WHERE [Order].Transaction=[OrderDetail].Transaction AND OrderNo='" & cboOrderNo.Text & "'", con)
- Dim ds As DataSet = New DataSet
- Dim trans As String = ""
- sadapt.Fill(ds)
- dgvDetails.Rows.Clear()
- dgvDetails.RowCount = 1
- For intRow = 0 To ds.Tables(0).Rows.Count - 1
- dgvDetails.Rows(intRow).Cells("SrNo").Value = ds.Tables(0).Rows(intRow).Item("SrNo").ToString
- dgvDetails.Rows(intRow).Cells("Particular").Value = ds.Tables(0).Rows(intRow).Item("Particular").ToString
- dgvDetails.Rows(intRow).Cells("Barcode").Value = ds.Tables(0).Rows(intRow).Item("PlainTees").ToString
- dgvDetails.RowCount = dgvDetails.RowCount + 1
... where con is my connection.
Actually, the problem was in the red colored line of code.
Each time we increment the row count of DataGridView, it's clearing out the data. But, this is not alway done.
So, the solution for such a problem is shown below.
- Dim sadapt As New OleDbDataAdapter("SELECT * FROM [OrderDetail],[Order] WHERE [Order].Transaction=[OrderDetail].Transaction AND OrderNo='" & cboOrderNo.Text & "'", con)
- Dim ds As DataSet = New DataSet
- Dim trans As String = ""
- sadapt.Fill(ds)
- dgvDetails.Rows.Clear()
- dgvDetails.RowCount = dgvDetails.RowCount + 1
- For intRow = 0 To ds.Tables(0).Rows.Count - 1
- dgvDetails.Rows(intRow).Cells("SrNo").Value = ds.Tables(0).Rows(intRow).Item("SrNo").ToString
- dgvDetails.Rows(intRow).Cells("Particular").Value = ds.Tables(0).Rows(intRow).Item("Particular").ToString
- dgvDetails.Rows(intRow).Cells("Barcode").Value = ds.Tables(0).Rows(intRow).Item("PlainTees").ToString
Conclusion
I hope this will help others who have been stuck in the same problem.