How to Refresh Datagrid Control?

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
 
F

Frank Uray

Hi

I think you first have to reload the DataSet,
the grid will refresh automaticaly when DataSet is changed.

Frank
 
M

Mark

Frank -

Would you suggest the code that I need to use to "refresh" the dataset?

I'm at a loss.

Thanks,
Mark
 
F

Frank Uray

Hi Mark

I do not know your code ...
Normaly you would fill a grid like this:

**************

// Variables
public System.Data.SqlClient.SqlConnection return_SQLNATIVEConnection = new
System.Data.SqlClient.SqlConnection();

// Create Connection
return_SQLNATIVEConnection.ConnectionString = "Data
Source=SQLServer\SQLInstance;Initial Catalog=SQLDatabase;Integrated
Security=SSPI;";
return_SQLNATIVEConnection.Open();

// DataAdapter erstellen
System.Data.SqlClient.SqlDataAdapter local_DataAdapter = new
System.Data.SqlClient.SqlDataAdapter("SELECT * FROM Table",
return_SQLNATIVEConnection);

// DataSet erstellen
System.Data.DataSet local_DataSet = new System.Data.DataSet();

// DataSet füllen
local_DataAdapter.SelectCommand.CommandTimeout = 0
local_DataAdapter.Fill(local_DataSet, "DataSetTableName");

// Grid füllen
this.YourDataGrid.DataSource = local_DataSet

**************

For reload/refresh just run this code.

Best regards
Frank
 
M

Mark

Frank -

Thanks for the code. I can use this (and probably will end up using it if I
can't figure it out (see below)).

In my initial question I mentioned that I used the Add New Data Source
wizard. This process created the connection and a typed dataset for me
without any coding from me at all --- which is great and nice. It seems to
me that I should be able to refresh the dataset somehow, leveraging the code
generated by the wizard.

For example, soemthing like this (which doesn't work) would be great.
local_Dataset.clear();
local_Dataset.mydatatableTableAdaptor.GetData();

So yeah I can use the code you supplied but then I wonder why I bothered
using the wizard in the first place.

Again, thanks for your suggestions.

Mark
 
F

Frank Uray

Hi Mark

Well, the wizard also just creates some code.
This code is possibly stored in FormXY.Designer.cs.
And it works, when you open the form, the data is upToDate.

Because a DataSet is something like a snapshot,
"this.YourGridView.Refresh()" is not working ...
It is refreshing against the DataSet and this is still the same.
You first have to bring your DataSet UpToDate.

Or you start using some external (not MS) components.
I use Infragistics for all grid applications. Makes life easyer ... :))

Regards
Frank
 
M

Mark

Frank -

Thanks for your comments.

The following code did the trick [i.e., requeried the sql db]:
this.mytableTableAdapter.Fill(this.myDataset.mytable);

In fact I had something like this yesterday but it did not work. In reality
it was working but I didn't think it was since something else wasn't
working... When I went back to a simple model "Fill" worked just fine, and I
was able to work around that something else.

Again thanks for your interest.

Mark
 

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