By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

How to Refresh Datagrid Control?

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi

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

Frank
"Mark" wrote:
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
Jun 27 '08 #2

P: n/a
Frank -

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

I'm at a loss.

Thanks,
Mark

"Frank Uray" wrote:
Hi

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

Frank
"Mark" wrote:
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
Jun 27 '08 #3

P: n/a
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

"Mark" wrote:
Frank -

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

I'm at a loss.

Thanks,
Mark

"Frank Uray" wrote:
Hi

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

Frank
"Mark" wrote:
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
Jun 27 '08 #4

P: n/a
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



"Frank Uray" wrote:
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

"Mark" wrote:
Frank -

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

I'm at a loss.

Thanks,
Mark

"Frank Uray" wrote:
Hi
>
I think you first have to reload the DataSet,
the grid will refresh automaticaly when DataSet is changed.
>
Frank
>
>
"Mark" wrote:
>
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
Jun 27 '08 #5

P: n/a
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

Jun 27 '08 #6

P: n/a
Frank -

Thanks for your comments.

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

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


"Frank Uray" wrote:
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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.