Refresh Bound DataGrid

M

Mark

Hi -

I'm having trouble refreshing a datagrid control bound to a dataset that was
created from the New Data Source wizard. What is the code required to
refresh the datagrid with data from the data source (sql db)?

More Details:

The application is C# Windows (VS 2005).

I used the "Add New Data Source" wizard to connect to a sql database and
select the tables to include in the in-memory dataset.

From the Data Source window, I selected a table, designated it as a datagrid
and then dragged it onto the form. So far so good. The form runs fine, the
data grid populates properly.

Now I want to refresh the datagrid control. (I know the sql db has updated
(values changed, new records inserted), after the form was started.) I have
added a button to the form (named "Refresh Grid") and want to add code to
it's click event to do the refreshing. My question is what is the code that
is needed to requery the sql database?

Thanks for your suggestions.
Mark
 
C

Cor Ligthert[MVP]

Mark,

As I see this problem then it is mostly that there is created a new
datasource, while an old one is still active because there is a reference.

As this is the fact, then clear that datasource instead of creating a new
one.

Cor
 
M

Mark

Cor -

Could you suggest some actual code?

Given that the wizard has generated all of the code to do the connection,
create the dataset bind the control to the dataset (not to mention my lack of
expertise with ado.net) I'm at a loss for the code.

Thanks again for your comments and help.

Mark
 
C

Cor Ligthert[MVP]

Mark,

I assume that this is a windows form datagrid as you wrote.

Assume that you do this somewhere.

dataGrid.DataSource = dataTable;
dataTable = new DataTable();

Then your dataGrid will show forever the old datatable, which will not be
disposed whatever you do until you do again.

dataGrid.Datasource = dataTable;

Cor
 
M

Mark

Cor -

Thanks for your comments.

Mark

Cor Ligthert said:
Mark,

I assume that this is a windows form datagrid as you wrote.

Assume that you do this somewhere.

dataGrid.DataSource = dataTable;
dataTable = new DataTable();

Then your dataGrid will show forever the old datatable, which will not be
disposed whatever you do until you do again.

dataGrid.Datasource = dataTable;

Cor
 

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

Top