Results 1 to 5 of 5

Thread: Datagrid (RESOLVED)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Datagrid (RESOLVED)

    My button_click event does not show datagrid when execute command. Instead I have to click on plus sign, then the word table to see the results in grid format. How can I modifiy code to
    show grid format went command is executed?

    The code below is driven by the combobox selection. However, I'm able to get it to execute properly on first try. Subsequent click event result in error "cannot creat child list for Apr04". Any ideas what's wrong with the code?

    thanks


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim DS As System.Data.DataSet
    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    Dim MyConnection As System.Data.OleDb.OleDbConnection

    MyConnection = New System.Data.OleDb.OleDbConnection( _
    "provider=Microsoft.Jet.OLEDB.4.0; " & _
    "data source=C:\Inetpub\wwwroot\intelis.MDB")

    MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
    "select * from APR04 where Entity = '" & ComboBox1.Text.Trim & "'", MyConnection)

    DS = New System.Data.DataSet
    DS.Clear()

    MyCommand.Fill(DS)

    DataGrid1.DataMember = "APR04"
    DataGrid1.DataSource = DS
    'MyConnection.Close()
    End Sub
    Last edited by Hutty; Jun 23rd, 2004 at 02:41 PM.

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    A dataset is a container for one or more datatables.
    The methods that attach a dataset to a datagrid do not know the number of tables your datagrid contains, so it just attaches the entire dataset to the grid, this is actually very nice behavior if you have multiple tables contained in your set and want to be able to view them in the grid without looping through the tables.

    Your solution is to create a datatable object and assign it to the table in your dataset, then assign the datagrids datasource to the datatable:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim DS As System.Data.DataSet
    3.     Dim DT As System.Data.DataTable
    4.     Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    5.     Dim MyConnection As System.Data.OleDb.OleDbConnection
    6.  
    7.     MyConnection = New System.Data.OleDb.OleDbConnection( _
    8.     "provider=Microsoft.Jet.OLEDB.4.0; " & _
    9.     "data source=C:\Inetpub\wwwroot\intelis.MDB")
    10.  
    11.     MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
    12.     "select * from APR04 where Entity = '" & ComboBox1.Text.Trim & "'", MyConnection)
    13.  
    14.     DS = New System.Data.DataSet
    15.     DS.Clear()
    16.  
    17.     MyCommand.Fill(DS)
    18.     DT = DS.Tables(0)
    19.  
    20.     DataGrid1.DataMember = "APR04"
    21.     DataGrid1.DataSource = DT
    22.     MyConnection.Close()
    23. End Sub

    Alternatively, you can fill your table instead of a dataset directly from your command:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim DT As System.Data.DataTable = New System.Data.DataTable
    3.     Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    4.     Dim MyConnection As System.Data.OleDb.OleDbConnection
    5.  
    6.     MyConnection = New System.Data.OleDb.OleDbConnection( _
    7.     "provider=Microsoft.Jet.OLEDB.4.0; " & _
    8.     "data source=C:\Inetpub\wwwroot\intelis.MDB")
    9.  
    10.     MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
    11.     "select * from APR04 where Entity = '" & ComboBox1.Text.Trim & "'", MyConnection)
    12.  
    13.     MyCommand.Fill(DT)
    14.  
    15.     DataGrid1.DataMember = "APR04"
    16.     DataGrid1.DataSource = DT
    17.     MyConnection.Close()
    18. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Datagrid

    Thanks CYBER! The datatable displays properly with the addition.

    Now, I still have the second issue still where when I click to execute the button_click on subsequent command I get the error"cannot create child list for field Apr04". Apr04 is the table name.

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    how do you know that your table is name Apr04?

    When you fill a dataset, the dataadapter automatically assigns names to tables like "Table", "Table2", etc...

    If you did not explicitly tell the dataadapter that your table name is "Apr04" then it isn't.

    try this code:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim DS As New System.Data.DataSet
    3.     Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    4.     Dim MyConnection As System.Data.OleDb.OleDbConnection
    5.  
    6.     DS.TableMappings.Add("Table", "APR04")
    7.     MyConnection = New System.Data.OleDb.OleDbConnection( _
    8.     "provider=Microsoft.Jet.OLEDB.4.0; " & _
    9.     "data source=C:\Inetpub\wwwroot\intelis.MDB")
    10.  
    11.     MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
    12.     "select * from APR04 where Entity = '" & ComboBox1.Text.Trim & "'", MyConnection)
    13.  
    14.     MyCommand.Fill(DS)
    15.  
    16.     DataGrid1.SetDataBinding(DS, "APR04")
    17.     MyConnection.Close()
    18. End Sub
    Last edited by CyberHawke; Jun 23rd, 2004 at 01:46 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153
    Thanks Cyber. It makes sense in logic. I just assumed the table name from trying it using the datagrid properties on the form. I took out the line "Datagrid1.DataMember ="apr04" and it works.

    thanks again.

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