Setting DataGridView's DataSource at runtime?

  • Thread starter Thread starter Sin Jeong-hun
  • Start date Start date
S

Sin Jeong-hun

Visual Studio automatically generates DateSet classes and all the
other classes automatically at the design time. But is it possible to
choose am MDB file at the rum time, and select a table to display, and
then display the table contents in the DataGridView?

I think, if I do that manually, I must first enumerates all the tables
in the MDB, and enumerates all the fileds in that table, and
dynamically create a DataSet. But can it be done automatically? Or is
there any quick and easy way to do this?

Thank you.
 
Sin Jeong-hun said:
Visual Studio automatically generates DateSet classes and all the
other classes automatically at the design time. But is it possible to
choose am MDB file at the rum time, and select a table to display, and
then display the table contents in the DataGridView?

I think, if I do that manually, I must first enumerates all the tables
in the MDB, and enumerates all the fileds in that table, and
dynamically create a DataSet. But can it be done automatically? Or is
there any quick and easy way to do this?

A quick and easy way to create a DataSet is to use a DataAdapter. When
you use the Fill method of the DataAdapter, the default behaviour is to
create a datatable into the dataset if it doesn't exist. This saves the part
of "enumerating the fields", but it still requires to enumerate the tables.

OleDbConnection cn = new OleDbConnection(connectionstring);
OleDbDataAdapter da = new OleDbDataAdapter("Select * from theTable", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataGridView1.DataSource = ds.Tables[0];

In the preceding code, there are two parts where you have to replace
data: Inside the connectionstring is the path of the MDB file; you will have
to change it to point to the MDB that you choose. And "theTable" that you
concatenate into the query in the dataadapter constructor will have to be a
variable that you choose from the list of tables in the MDB.
 
Visual Studio automatically generates DateSet classes and all the
other classes automatically at the design time. But is it possible to
choose am MDB file at the rum time, and select a table to display, and
then display the table contents in the DataGridView?
I think, if I do that manually, I must first enumerates all the tables
in the MDB, and enumerates all the fileds in that table, and
dynamically create a DataSet. But can it be done automatically? Or is
there any quick and easy way to do this?

A quick and easy way to create a DataSet is to use a DataAdapter. When
you use the Fill method of the DataAdapter, the default behaviour is to
create a datatable into the dataset if it doesn't exist. This saves the part
of "enumerating the fields", but it still requires to enumerate the tables.

OleDbConnection cn = new OleDbConnection(connectionstring);
OleDbDataAdapter da = new OleDbDataAdapter("Select * from theTable", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataGridView1.DataSource = ds.Tables[0];

In the preceding code, there are two parts where you have to replace
data: Inside the connectionstring is the path of the MDB file; you will have
to change it to point to the MDB that you choose. And "theTable" that you
concatenate into the query in the dataadapter constructor will have to be a
variable that you choose from the list of tables in the MDB.

Thank you, Mr. Alberto Poblacion. I'll try your code. It will be a
lot easier than my original idea.
Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top