I have a datagridview (with bindingsource, a detail from master) which is populated programmatically from datareader. Datareader successfully retrieves all data and add new rows to bindingsource. However the datagridview is only displaying one row. So in the datagridview it is displaying the last row from the datareader.
When I click Previous button from the bindingnavigator (of the master), then Next, all rows are displayed in the datagridview as if it is refreshing the bindingsource and binding each rows to each datagridview rows. I know I'm missing something here, but not sure what it is.
Private Sub PopulateDetails(ByVal orderno As String)
Dim cmd As New SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
LoadDB()
If Not con.State = ConnectionState.Open Then
con.Open()
End If
cmd.CommandText = "select orderid, pono, cono, styleno, fabriccodeid, custfabriccode, colorid, orderqty, totalbales, uomid, width, " &
"weightperarea, yard, gramperyard, size, unitprice, currencycode, deliverydt, remark from tblorderdetails " &
"where orderno = @orderno;"
cmd.Connection = con
cmd.Prepare()
cmd.Parameters.AddWithValue("@orderno", orderno)
dr = cmd.ExecuteReader
With dr
If .HasRows() Then
Dim newRow As DataRowView
While .Read
newRow = DirectCast(TblorderdetailsBindingSource.AddNew(), DataRowView)
newRow.BeginEdit()
newRow.Row.BeginEdit()
newRow.Row("orderno") = OrdernoTextBox.Text
newRow.Row("pono") = .Item("pono")
newRow.Row("cono") = .Item("cono")
newRow.Row("styleno") = .Item("styleno")
newRow.Row("fabriccodeid") = .Item("fabriccodeid")
newRow.Row("custfabriccode") = .Item("custfabriccode")
''newRow("btncolor") = .Item("colorid")
newRow.Row("orderqty") = .Item("orderqty")
newRow.Row("totalbales") = .Item("totalbales")
newRow.Row("uomid") = .Item("uomid")
newRow.Row("width") = .Item("width")
newRow.Row("weightperarea") = .Item("weightperarea")
newRow.Row("yard") = .Item("yard")
newRow.Row("gramperyard") = .Item("gramperyard")
newRow.Row("size") = .Item("size")
newRow.Row("unitprice") = .Item("unitprice")
newRow.Row("currencycode") = .Item("currencycode")
newRow.Row("deliverydt") = .Item("deliverydt")
newRow.Row("remark") = .Item("remark")
newRow.Row.EndEdit()
newRow.DataView.Table.Rows.Add(newRow.Row)
End While
End If
.Close()
End With
con.Close()
End Sub
I've been working for 4 days finding ways to show all rows in the datagridview, and finally resolve it by this:
TblorderBindingSource.MovePrevious()
TblorderBindingSource.MoveNext()
Apparently the bindingsource needs to be refreshed somehow by the bindingnavigator then only it shows all the added rows in datagridview (with the autogenerated column id of -1,-2,-3...). User can amend these rows then save.
I regret that this does not seems like an "intelligent" workaround, but glad it works somehow.