Filling A Datagrid On Another Form . . .
Hey all,
I have a dataset of search results that I want to put into a datagrid on another form, then allow the user to select one row and fill in textboxes on the main form with that row's data.
Here's what I have so far...this is the code on the main form for my search button. It works fine filling in the textboxes from the dataset on the form this button is on, I just need to know how to populate the datagrid(grdCustSearch) in the other form(frmCustSearch) with the first two columns of dss:
VB Code:
Private 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 frmCustSearch
Try
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")
'Something to fill the datagrid in the other form goes here; I just KNOW it!
'These were used to fill in the textboxes on the current form
'And they still will be, but from the dataset on the new form
'txtCustomerName.Text = dss.Tables("customer").Rows(0).Item(0)
'txtCustomerNumber.Text = dss.Tables("customer").Rows(0).Item(1)
csreturn.ShowDialog()
csreturn.Dispose()
Catch ex As Exception
MessageBox.Show("No record found", "No Record", MessageBoxButtons.OK)
End Try
End Sub
Thanks in advance. :wave:
CP
Re: Filling A Datagrid On Another Form . . .
first make a datatable to bind to your datagrid in another form
VB Code:
Private 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 frmCustSearch
Try
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")
'Something to fill the datagrid in the other form goes here; I just KNOW it!
txtCustomerName.Text = dss.Tables("customer").Rows(0).Item(0)
txtCustomerNumber.Text = dss.Tables("customer").Rows(0).Item(1)
dim dt as new datatable()
dim dr as datarow=dt.newrow()
dr(0)=txtCustomerName.Text
dr(1)=txtCustomerNumber.Text
csreturn.grdCustSearch.DataSource=dt
csreturn.ShowDialog()
csreturn.Dispose()
Catch ex As Exception
MessageBox.Show("No record found", "No Record", MessageBoxButtons.OK)
End Try
End Sub
hope that helps.
Re: Filling A Datagrid On Another Form . . .
Thanks for the reply mar_zim, I'm getting a 'System.IndexOutOfRangeException' error that says "Cannot Find Column 0" and highlites in green the line below:
dr(0)=txtCustomerName.Text
Did I miss something?
Also, is there a way to fill that datagrid from the existing dataset (dss), instead of creating a datatable, or filling the datatable from dss instead of the two TextBoxes ?
Thanks,
CP
Re: Filling A Datagrid On Another Form . . .
I found a shorter, more efficient way without even using a DataTable. It returns all the fields for the customers searched, but that's ok. I just changed the DataSource for the DataGrid on the other form to be dss :
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 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")
csreturn.grdCustSearchResults.DataSource = dss.Tables("customer")
csreturn.ShowDialog()
csreturn.Dispose()
End Sub
Re: Filling A Datagrid On Another Form . . .
Now to get the data from the selected row in the DataGrid back to the main form to fill in the textboxes. I don't see a SelectedRow, or SelectedSomethingChanged available. Help please.