|
-
May 23rd, 2005, 05:16 PM
#1
Thread Starter
Lively Member
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. 
CP
Last edited by milkmood; May 23rd, 2005 at 05:43 PM.
-
May 23rd, 2005, 08:56 PM
#2
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.
-
May 24th, 2005, 08:26 AM
#3
Thread Starter
Lively Member
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
Last edited by milkmood; May 24th, 2005 at 08:46 AM.
-
May 24th, 2005, 09:49 AM
#4
Thread Starter
Lively Member
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
Last edited by milkmood; May 24th, 2005 at 10:51 AM.
-
May 24th, 2005, 11:03 AM
#5
Thread Starter
Lively Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|