Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Improved question
=============
How do I simply display a DataTable within a DataGridView without binding the former to the later (using DataSource or so)?

Thanks!

Original question
============

Hi,
I've a DataGridView (say dgv) with the DataSource property set to a DataTable (say dt). 'dt' keeps getting updated asynchronously from some other sources/actions. As and when 'dt' gets updated, CLR triggers for the 'dgv' to also get updated. The problem is, if the 'dt' updates are very frequent, then it slows down the GUI containing 'dgv' because of the DataSource binding of the 'dgv' to 'dt', and even sometimes makes it hang.

Hence, I want to either control the frequency of the 'dgv' updates, or better, I'd prefer the user of the GUI to manually ask for the updated display of the 'dgv' pertaining to any intermittent changes in the 'dt'. I can do this by providing a button just for this purpose. Also, between any two subsequent refreshes of the 'dgv', I'd prefer that the 'dgv' keeps displaying the previous refreshed data from 'dt'. That's to say I won't like to write,

private void button_Click(...)
{
dgv.DataSource = null;
// or, dgv.DataSource = typeof(DataTable), for retaining the column structure, right?
dgv.DataSource = dt;
}

because then, between two subsequent refreshes 'dgv' will be shown as empty.

Any suggestions for achieving this?

Thanks!
Posted
Updated 8-Dec-10 19:08pm
v2

Why not just omit: dgv.DataSource = null ?
 
Share this answer
 
Comments
kjain18 8-Dec-10 22:28pm    
Thanks for the comment; but, it wouldn't work. As soon as the DataSource property is set, it becomes 'persistent', that is all the new updates get automatically reflected in the 'dgv'. The event handler I showed was just representative... what I want to achieve is to set the display of the 'dgv' to 'dt' *selectively*.
Use Ajax Update panel control and timer control....place the GridView inside the update panel, define a time interval for the timer control and trigger the event to refresh the GridView...you will get your desired result....hope makes sense...
 
Share this answer
 
Comments
kjain18 8-Dec-10 22:31pm    
I wouldn't want to use web-development related technologies for this, as it'd unnecessarily amount to adding so many extra libraries just for this purpose. I'd prefer to use something more 'native' to C# WinForm programming. Thanks anyway for your response!
senguptaamlan 9-Dec-10 1:28am    
I don't think this can be achieved without using the DataSource

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900