Is it possible to use a selected row in a DataGrid to fill in textboxes from the fields? If so, how.
Thanks,
CP
Printable View
Is it possible to use a selected row in a DataGrid to fill in textboxes from the fields? If so, how.
Thanks,
CP
im not too sure if this is what u want.give it a try
0=first column in datagridVB Code:
textBoxr1.text=datagrid.Item(datagrid.CurrentRowIndex, 0)
1=second
.....
We're on the right track now...I get:
An unhandled exception of type 'System.NullReferenceException' occurred in MyApplication.exe
Additional information: Object reference not set to an instance of an object.
And it hi-lites the first line in yellow. If I comment out the first line, it hi-lites the second line, and so on.
'pm' is the main form of my solution (not the one in focus now), the form I'm working on has a datagrid with a select and a cancel button beneath it. The idea is to select a row in the datagrid, and move the data in that row to the textboxes on the main form. I suspect I'm handling the move to the main form wrong, but I'm not sure.
VB Code:
Private Sub btnCustSearchSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustSearchSelect.Click Dim pm As PartsManager 'Crashes on this line first pm.txtCustomerName.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 0) pm.txtCustomerNumber.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 1) pm.txtCustomerAddress.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 2) pm.txtCustomerAddress2.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 3) pm.txtCustomerPOBox.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 4) pm.txtCustomerCity.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 5) pm.txtCustomerState.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 6) pm.txtCustomerZip.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 7) pm.txtCustomerContact.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 8) pm.txtCustomerPhone.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 9) pm.txtCustomerNotes.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 10) pm.txtCustomerLastUpdate.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 11) pm.txtCustomerUpdatedBy.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 12) pm.chkCustShipTo.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 13) pm.chkCustKeepInventory.Text = grdCustSearchResults.Item(grdCustSearchResults.CurrentRowIndex, 14) Me.Close() End Sub
I got it. Geez, I'm solving all these problems myself. Maybe I should be the MVP.
I ended up getting the data back to the parent form using code in the parent from instead of pushing it to the parent form from the child form with the datagrid.
Simpler than beans, there's a button on the child form that says "Select" but all it really does is closes the child form because the data has already been returned to the parent form once a row is selected :p ; here's the whole routine:
VB Code:
Public Sub btnCustomerSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomerSearch.Click Dim srch As String Dim sqls As String Dim dss As New DataSet Dim das As New Odbc.OdbcDataAdapter Dim csreturn As New PartsLifeCycleManager.frmCustSearch srch = txtCustomerSearch.Text sqls = "SELECT * FROM customer WHERE LOWER(customername) LIKE %" & srch.ToLower & "% ORDER BY customername ASC;" das = New Odbc.OdbcDataAdapter(sqls, con) dss.Clear() das.Fill(dss, "customer") If dss.Tables("customer").Rows.Count < 1 Then MessageBox.Show("No record found", "No Record", MessageBoxButtons.OK) Exit Sub Else csreturn.grdCustSearchResults.DataSource = dss.Tables("customer") csreturn.ShowDialog() txtCustomerName.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 0) txtCustomerNumber.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 1) txtCustomerAddress.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 2) txtCustomerAddress2.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 3) txtCustomerPOBox.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 4) txtCustomerCity.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 5) txtCustomerState.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 6) txtCustomerZip.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 7) txtCustomerContact.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 8) txtCustomerPhone.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 9) txtCustomerNotes.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 10) txtCustomerLastUpdate.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 11) txtCustomerUpdatedBy.Text = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 12) chkCustShipTo.Checked = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 13) chkCustKeepInventory.Checked = csreturn.grdCustSearchResults.Item(csreturn.grdCustSearchResults.CurrentRowIndex, 14) csreturn.Dispose() das.Dispose() dss.Dispose() End If End Sub