views:

1668

answers:

1

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 add the feature where the user can select the table name from the combo and then it should populate the contents of that table.

I tried the following Code

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

however that did not work and i was only getting the column headers. Then i read further that i needed a TableAdapter to populate the dataset with that table. When i encountered this i hit a road block, because then for every table i will have to use it's datatableadapter object and i won't be able to do it 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. Similarly i will have to do it again for "TableB". Is there not a generic methoed to populate any table in the dataset?

A: 

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).

Sorin Comanescu
the solution seems elegant, but then may i ask why such a thing can't be done through an existing dataset? if all the table adapter does is to fill in the dataset with the table, and that too us specifying the query, there should be a way for doing via dataset as well..
Anirudh Goel