Auto Loading Data into the DataGridView
-
2012년 7월 29일 일요일 오후 5:00
Hi,
Having did a tremndous effort to find out a solution to my problem ulimatly I would like to post my problem for a solution please.
I have a DataGridView working as a detail form in a master-detail relationship.
The columns are "GRNNO"(Forign key for the master form),"ProductID", "ReceivedQty", "UnitPrice"
In the master form I have "GRNNO" (Primary Key) , and "OrderNo" (I have mentioned only the required field for the problem to be demonstrated)
My problem is in the form when I selected the OrderNo the corresponding ProductID,OrderQty as receivedQty,OrderPrice as UnitPrice should be diplayed in the DataGridView.
Please note the OrderDetails are stored in a separate table and the Qty Received detail in another table.
Please give me solution with a sample code.
모든 응답
-
2012년 7월 29일 일요일 오후 5:10
One question: how do you populate datagridview control? Do you databind it, for datatables or something?
Can you provide us any code you have there? Would be way easier to help you out.
thx
Mitja
-
2012년 7월 29일 일요일 오후 5:50
Yes
Here is the code that I have tried with.
AdapterGRNMaster.SelectCommand = New SqlCommand("Select * from GRN", GRNSqlCon) AdapterGRNMaster.FillSchema(DataSetGRN, SchemaType.Source, "GRN") AdapterGRNMaster.Fill(DataSetGRN, "GRN") AdapterGRNDetail.SelectCommand = New SqlCommand("Select * from GRNDetail", GRNSqlCon) AdapterGRNDetail.FillSchema(DataSetGRN, SchemaType.Source, "GRNDetail") AdapterGRNDetail.Fill(DataSetGRN, "GRNDetail") Dim relation As New DataRelation("myRelation", DataSetGRN.Tables("GRN").Columns("GRNNO"), DataSetGRN.Tables("GRNDetail").Columns("GRNNO")) BSGrn.DataSource = DataSetGRN BSGrn.DataMember = "GRN" BSGRNDet.DataSource = BSGrn BSGRNDet.DataMember = "myRelation" txtGRNNO.DataBindings.Add("Text", BSGrn, "GRNNO") txtGRNDate.DataBindings.Add("Text", BSGrn, "GRNDate") txtInvoiceNo.DataBindings.Add("Text", BSGrn, "InvoiceNo") Dim i As Integer Dim SupSelID As Integer Dim copy As New DataTable cmbPONO.Text = "" For i = 0 To DataSetGRN.Tables("Supplier").Rows.Count If (cmbSuppID.ValueMember) IsNot Nothing Then SupSelID = cmbSuppID.SelectedValue Dim cmd As New SqlClient.SqlCommand("Select Distinct OrderNo from Orderr where SuppID=" & "'" & SupSelID & "'", GRNSqlCon) AdapterPoNoSel.SelectCommand = cmd DataSetGRN.EnforceConstraints = False AdapterPoNoSel.FillSchema(DataSetOrderNo, SchemaType.Source, "Orderr") AdapterPoNoSel.Fill(DataSetOrderNo, "Orderr") copy = DataSetOrderNo.Tables("Orderr").Copy cmbPONO.DisplayMember = "OrderNo" cmbPONO.ValueMember = "OrderNo" cmbPONO.DataSource = copy DataSetOrderNo.Tables("Orderr").Clear() End If Next cmbPONO.DataBindings.Clear() cmbPONO.DataBindings.Add("SelectedValue", BSGrn, "OrderNo") Dim FilterNo As Integer FilterNo = cmbPONO.SelectedValue Dim intCount As Integer DataSetViewGRNBal.Clear() AdapterViewGRNBal.SelectCommand = New SqlCommand("Select * from ViewGRNBal where OrderNo=" & "'" & FilterNo & "'", GRNSqlCon) AdapterViewGRNBal.Fill(DataSetViewGRNBal, "ViewGRNBal") For intCount = 0 To Me.DataSetViewGRNBal.Tables("ViewGRNBal").Rows.Count - 1 DGVGRN.Rows(intCount).Cells("ProductID").Value = DataSetViewGRNBal.Tables("ViewGRNBal").Rows(intCount).Item("ProductID") intCount += 1 Next
-
2012년 7월 30일 월요일 오전 1:51
I think the above whole code is confusing and I will post only a code to enable every one understand the requirement.
The below code is to get the Order detail into the DGVGRN.But I am getting the error message 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound'.
Pls help !
For i As Integer = 0 To Me.DataSetViewGRNBal.Tables("ViewGRNBal").Rows.Count - 1 DGVGRN.DataSource = "" Dim item As New DataGridViewRow item.CreateCells(DGVGRN) With item .Cells(0).Value = DataSetViewGRNBal.Tables("ViewGRNBal").Rows(i).Item(3) .Cells(1).Value = DataSetViewGRNBal.Tables("ViewGRNBal").Rows(i).Item(2) .Cells(2).Value = DataSetViewGRNBal.Tables("ViewGRNBal").Rows(i).Item(4) End With DGVGRN.Rows.Add(item) Next
- 편집됨 wasantha Bandara Rambukwella 2012년 7월 30일 월요일 오전 1:55
-
2012년 7월 30일 월요일 오전 10:01
I got the following codes in 'c' and 'vb' from a old post as a solution to add rows progrmatically to a datagridview which is bounded.Could any one explain the VB.Code not 'C' code more understandable way ?
Thanks in advance.
// DataGridView will generate a new row automatically SampleDataSet.CustomersRow newCustomersRow = sampleDataSet.Customers.NewCustomersRow(); newCustomersRow.CustomerID = "1234-123"; newCustomersRow.CompanyName = "Sample Works"; sampleDataSet.Customers.Rows.Add(newCustomersRow); Sorry I just realize the above code was in C and not VB which generated an error combined with the fact that everyone was saying it works great. Using the parameters in the DataSet I came up with this for VB Dim newCustomersRow As SampleDataSet.CustomersRow newCustomersRow = SampleDataSet.Customers.NewCustomersRow newCustomersRow.CustomerID = "1234" newCustomerRow.CompanyName = "Sample Works" SampeDataSet.Customers.Rows.Add(newCustomersRow)
- 편집됨 wasantha Bandara Rambukwella 2012년 7월 30일 월요일 오전 10:04
- 답변으로 표시됨 wasantha Bandara Rambukwella 2012년 7월 31일 화요일 오전 3:24
- 답변으로 표시 취소됨 wasantha Bandara Rambukwella 2012년 7월 31일 화요일 오전 3:25
- 답변으로 표시됨 wasantha Bandara Rambukwella 2012년 7월 31일 화요일 오전 3:27
-
2012년 7월 31일 화요일 오전 3:30
This was the answer
Dim newCustomersRow As DataRow newCustomersRow = SampleDataSet.Tables("Customers").NewRow newCustomersRow.Items("CustomerID") = "1234" newCustomerRow.Items("CompanyName") = "Sample Works" SampeDataSet.Tables("Customers").Rows.Add(newCustomersRow)

