1
votes

I am working in VB.NET and I have a simple requirement:

I have added a MDB file to a DataSet and it contains 21 tables.

I have a DataGridView and a ComboBox on my form.

I was able to get the ComboBox populated with the table names available in the DataSet by iterating through dataset.Tables.

Now I want to the user to be able to select the table name from the ComboBox and then populate the contents of that table.

I tried the following code:

Datagridview1.DataSource = dataset1
Datagridview1.DataMember = dataset1.tables(combobox1.selecteditem)
Datagridview1.Refresh()

But I only got the column headers. Then I read that I need a TableAdapter to populate the DataSet with that table. But if I use the TableAdapter then I won't be able to populate the table in a generic way.

Currently if I have to populate TableA then I will have to create an instance of Dataset1TableAdapters.TableA and then use it's .Fill property to populate the table. I will also have to use "Dataset1TableAdapters.TableB`. Is there a generic method to populate any table in the DataSet?

4
Sorry, but I don't think the tag generic-way-to-get-tablea should stay... - Zifre
no probs, i was just trying to improve visibility to the question. - Anirudh Goel

4 Answers

2
votes

The following worked for me:

Datagridview1.DataSource = dataset1.tables(combobox1.selecteditem)
1
votes

C# but easily convertible to VB.Net (I suppose):

String connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\test.mdb""";
String tableName = combobox1.SelectedItem.ToString();
String sqlSELECT = String.Format("select * from [{0}]", tableName);

System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
da.SelectCommand = new System.Data.OleDb.OleDbCommand(sqlSELECT, new System.Data.OleDb.OleDbConnection(connectionString));

DataGridView1.DataSource = dataSet1;
DataGridView1.DataMember = dataSet1.Tables(tableName);
da.Fill(dataSet1, tableName);

I was just sketching a proof that "it can be done in a generic way" to your problem, you should elaborate and maybe come with something more elegant (and safe).

0
votes
Private Sub BindData()

    With customersDataGridView
        .AutoGenerateColumns = True
        .DataSource = customersDataSet
        .DataMember = "Customers"
    End With

End Sub
0
votes

Just to clarify this answer. For Future Reference.

Public Class Form1   
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\test.accdb;")

Private Sub Button1_Click

            Dim da As New OleDbDataAdapter
            Dim table As String = ComboBox1.SelectedItem.ToString
            Dim sql As String = String.Format("select * from [{0}]", table)
            da.SelectCommand = New OleDbCommand(sql, con)
            DataGridView1.DataSource = DataSet.Tables(table)
            da.Fill(DataSet, table)

End Sub
End Class

Not trying to Ressurect, but i thought the working version of this VB Script should be posted.

I would like to point out that the dataset is held by Visual Studio, hence the strange source string.