DataGrid class (System.Windows.Forms)

  • Thread starter Thread starter Sanddevil
  • Start date Start date
S

Sanddevil

Hi there - I hope someone out there can help me! I'm using a .Net DataGrid
Class to show the results of a SQL query in a spreadsheet type control. The
code, which works fine is:

iRowCount = oleDbDataAdapter1->Fill(dataSet1);

dataGrid1->DataSource = dataSet1->Tables->Item[0]->DefaultView;



I then update the underlying data in the database elsewhere in the code by
adding
some new rows and
want the datagrid control to show the updated and new rows correctly. I do a
refresh
to ensure the dataSet is up-to-date with the following statement:

iRowCount = oleDbDataAdapter1->Fill(dataSet1);

and having checked the iRowCount value I know that the dataSet has
definitely got the new rows.

So, the question is, how do I get the DataGrid to show the updated data on
the screen? There doesn't appear to be a Reload / Rebind type method in the
System.Windows.Forms.DataGrid Class. The Refresh() method doesn't do it. Any
help will be most appreciated!



Cheers
 
Hi Sanddevil,

Sanddevil said:
Hi there - I hope someone out there can help me! I'm using a .Net DataGrid
Class to show the results of a SQL query in a spreadsheet type control.
The
code, which works fine is:

iRowCount = oleDbDataAdapter1->Fill(dataSet1);

dataGrid1->DataSource = dataSet1->Tables->Item[0]->DefaultView;

I then update the underlying data in the database elsewhere in the code by
adding
some new rows and
want the datagrid control to show the updated and new rows correctly. I do
a
refresh
to ensure the dataSet is up-to-date with the following statement:

iRowCount = oleDbDataAdapter1->Fill(dataSet1);

and having checked the iRowCount value I know that the dataSet has
definitely got the new rows.

So, the question is, how do I get the DataGrid to show the updated data on
the screen? There doesn't appear to be a Reload / Rebind type method in
the
System.Windows.Forms.DataGrid Class. The Refresh() method doesn't do it.
Any
help will be most appreciated!


Although I'm not sure, I think "Invalidate" method could do that. What I
also think is that there is probably still a better way, but I never did
what you do. Invalidate actually forces the control to repaint itself.

Another thing I haven't tested but that might work, is to use the
SuspendBinding and ResumeBinding methods of the BindingManager of your grid.
Call SuspendBinding before modifying the DataSet and ResumeBinding after
that. You can retrieve the BindingManager using the BindingContext property
of your DataGrid:

BindingManagerBase bm;
bm = dataGrid.BindingContext[ dataGrid.DataSource,
dataGrid.DataMember ];
bm.SuspendBinding();
// update data
bm.ResumeBinding();

An interesting FAQ that might help you further is
http://msdn.microsoft.com/smartclient/community/wffaq/default.aspx (main
page)
and in specific
http://msdn.microsoft.com/smartclient/community/wffaq/ctrlsp.aspx

Kind regards,
Tom T.
 
"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
schreef in bericht [...]
Another thing I haven't tested but that might work, is to use the
SuspendBinding and ResumeBinding methods of the BindingManager of your
grid. Call SuspendBinding before modifying the DataSet and ResumeBinding
after that. You can retrieve the BindingManager using the BindingContext
property of your DataGrid:

BindingManagerBase bm;
bm = dataGrid.BindingContext[ dataGrid.DataSource,
dataGrid.DataMember ];
bm.SuspendBinding();
// update data
bm.ResumeBinding();
[...]

This example uses C#, just to mention... But it should be similar in MC++

TT
 
Hi Tom - many thanks for taking the trouble to respond to me. It was much
appreciated, and your suggestion worked!

The syntax was a little tricky in C++, so for the benfit of anyone stumbling
upon this in the future:

BindingManagerBase* pBm;

pBm = dataGrid1->BindingContext->Item[dataGrid1->DataSource,
dataGrid1->DataMember];

pBm->SuspendBinding();

// Do your updates to the underlying database here

pBm->ResumeBinding();


Many thanks again.
Cheers
Sanddevil



TT (Tom Tempelaere) said:
"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
schreef in bericht [...]
Another thing I haven't tested but that might work, is to use the
SuspendBinding and ResumeBinding methods of the BindingManager of your
grid. Call SuspendBinding before modifying the DataSet and ResumeBinding
after that. You can retrieve the BindingManager using the BindingContext
property of your DataGrid:

BindingManagerBase bm;
bm = dataGrid.BindingContext[ dataGrid.DataSource,
dataGrid.DataMember ];
bm.SuspendBinding();
// update data
bm.ResumeBinding();
[...]

This example uses C#, just to mention... But it should be similar in MC++

TT
 
Way to go Sanddevil...

TT

Sanddevil said:
Hi Tom - many thanks for taking the trouble to respond to me. It was much
appreciated, and your suggestion worked!

The syntax was a little tricky in C++, so for the benfit of anyone
stumbling
upon this in the future:

BindingManagerBase* pBm;

pBm = dataGrid1->BindingContext->Item[dataGrid1->DataSource,
dataGrid1->DataMember];

pBm->SuspendBinding();

// Do your updates to the underlying database here

pBm->ResumeBinding();


Many thanks again.
Cheers
Sanddevil



TT (Tom Tempelaere) said:
"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
schreef in bericht [...]
Another thing I haven't tested but that might work, is to use the
SuspendBinding and ResumeBinding methods of the BindingManager of your
grid. Call SuspendBinding before modifying the DataSet and
ResumeBinding
after that. You can retrieve the BindingManager using the
BindingContext
property of your DataGrid:

BindingManagerBase bm;
bm = dataGrid.BindingContext[ dataGrid.DataSource,
dataGrid.DataMember ];
bm.SuspendBinding();
// update data
bm.ResumeBinding();
[...]

This example uses C#, just to mention... But it should be similar in MC++

TT
 
In fact, I've come across another solution which I'll preserve here for
prosterity in case others need it.

This solution involves clearing the dataset and then reloading it. No
performance issues either - on my creaky old laptop it is a blink of an eye.

// Load the grid

iRowCount = oleDbDataAdapter1->Fill(dataSet1);

dataGrid1->DataSource = dataSet1->Tables->Item[0]->DefaultView;

// add / amend rows in the underlying database elsewhere in the code

// update the screen
dataSet1->Clear();

iRowCount = oleDbDataAdapter1->Fill(dataSet1);



Cheers

Sanddevil
 
Back
Top