Results 1 to 5 of 5

Thread: Filling A Datagrid On Another Form . . .

  1. #1

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    Question 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:
    1. Private Sub btnCustomerSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomerSearch.Click
    2.         Dim srch As String
    3.         Dim sqls As String
    4.         Dim dss As New DataSet
    5.         Dim das As New Odbc.OdbcDataAdapter
    6.         Dim csreturn As New frmCustSearch
    7.  
    8.         Try
    9.             srch = txtCustomerSearch.Text
    10.             sqls = "SELECT * FROM customer WHERE LOWER(customername) LIKE '%" & srch.ToLower & "%' ORDER BY customername ASC;"
    11.             das = New Odbc.OdbcDataAdapter(sqls, con)
    12.             dss.Clear()
    13.             das.Fill(dss, "customer")
    14.  
    15.                'Something to fill the datagrid in the other form goes here; I just KNOW it!
    16.  
    17.  
    18.                'These were used to fill in the textboxes on the current form
    19.                'And they still will be, but from the dataset on the new form
    20.             'txtCustomerName.Text = dss.Tables("customer").Rows(0).Item(0)
    21.             'txtCustomerNumber.Text = dss.Tables("customer").Rows(0).Item(1)
    22.  
    23.             csreturn.ShowDialog()
    24.             csreturn.Dispose()
    25.  
    26.         Catch ex As Exception
    27.             MessageBox.Show("No record found", "No Record", MessageBoxButtons.OK)
    28.         End Try
    29.  
    30.     End Sub
    Thanks in advance.
    CP
    Last edited by milkmood; May 23rd, 2005 at 05:43 PM.

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Filling A Datagrid On Another Form . . .

    first make a datatable to bind to your datagrid in another form
    VB Code:
    1. Private Sub btnCustomerSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomerSearch.Click
    2.         Dim srch As String
    3.         Dim sqls As String
    4.         Dim dss As New DataSet
    5.         Dim das As New Odbc.OdbcDataAdapter
    6.         Dim csreturn As New frmCustSearch
    7.  
    8.         Try
    9.             srch = txtCustomerSearch.Text
    10.             sqls = "SELECT * FROM customer WHERE LOWER(customername) LIKE '%" & srch.ToLower & "%' ORDER BY customername ASC;"
    11.             das = New Odbc.OdbcDataAdapter(sqls, con)
    12.             dss.Clear()
    13.             das.Fill(dss, "customer")
    14.  
    15.                'Something to fill the datagrid in the other form goes here; I just KNOW it!
    16.  
    17.             txtCustomerName.Text = dss.Tables("customer").Rows(0).Item(0)
    18.             txtCustomerNumber.Text = dss.Tables("customer").Rows(0).Item(1)
    19. dim dt as new datatable()
    20. dim dr as datarow=dt.newrow()
    21. dr(0)=txtCustomerName.Text
    22. dr(1)=txtCustomerNumber.Text
    23. csreturn.grdCustSearch.DataSource=dt
    24.  
    25.             csreturn.ShowDialog()
    26.             csreturn.Dispose()
    27.  
    28.         Catch ex As Exception
    29.             MessageBox.Show("No record found", "No Record", MessageBoxButtons.OK)
    30.         End Try
    31.  
    32.     End Sub

    hope that helps.

  3. #3

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    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.

  4. #4

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    Thumbs up 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:
    1. Public Sub btnCustomerSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomerSearch.Click
    2.         Dim srch As String
    3.         Dim sqls As String
    4.         Dim dss As New DataSet
    5.         Dim das As New Odbc.OdbcDataAdapter
    6.         Dim csreturn As New frmCustSearch
    7.  
    8.         srch = txtCustomerSearch.Text
    9.         sqls = "SELECT * FROM customer WHERE LOWER(customername) LIKE '%" & srch.ToLower & "%' ORDER BY customername ASC;"
    10.         das = New Odbc.OdbcDataAdapter(sqls, con)
    11.         dss.Clear()
    12.         das.Fill(dss, "customer")
    13.  
    14.         csreturn.grdCustSearchResults.DataSource = dss.Tables("customer")
    15.         csreturn.ShowDialog()
    16.         csreturn.Dispose()
    17.  
    18. End Sub
    Last edited by milkmood; May 24th, 2005 at 10:51 AM.

  5. #5

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    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
  •  



Click Here to Expand Forum to Full Width