473,756 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView refresh problem (DataTable)

Hi,

I have been encountering a refresh problem with DataGridView, which is bound
to a DataTable.

When I make updates (Add, Delete, update) to the DataGridView, everything
flow nicely to DataTable. No problem here.

However, when I add data (programaticall y) to the DataTable, the
DataGridView does not refresh right away. If I minimize and show my form,
the new rows are now shown in my form.

Yet, when I select the new rows, the "selection triangle" is not displayed
properly on the new rows. The selection works fine with the original rows.

I made an attempt to solve the problem by using a delegate to manually
perform refresh. I solve the "non-displaying" problem, but still have the
"selection" problem.

I also notice that, as soon as I delete or cell-update a row, all these
problems go away.

Is this a bug in .Net 2.0?

Thanks,
--
George
Jun 20 '06 #1
6 50472
Hi Jeffrey,

My code is similar to yours, with one exception.

You activate the DataTable.Rows. Add() from a control (button).

I have a FileSystemWatch er which detects a new input file. When a new file
appears, my code reads this file and append new data to my DataTable. Hence,
there is no interaction to the DataGridView.

I suspect the "in-activity" is causing the refresh problem. As I stated in
my first e-mail. As soon as I "interactiv ely" delete a row, everything is
fine.

Any ideas?

Thanks,


--
George
""Jeffrey Tan[MSFT]"" wrote:
Hi George,

Thanks for your post!

I have written a sample project regarding your scenario, however, it works
well on my side:

DataTable dt;
private void Form1_Load(obje ct sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add( "column1", typeof(int));
dt.Columns.Add( "column2", typeof(string)) ;

for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr) ;
}
this.dataGridVi ew1.DataSource = dt;
}

private void button1_Click(o bject sender, EventArgs e)
{
DataRow dr=this.dt.NewR ow();
dr["column1"] = 1000;
dr["column2"] = "NewItem";
dt.Rows.Add(dr) ;
}
When I clicked Button1 to add a new row to the DataTable, the DataGridView
refresh the update immediately.

I have attached my sample project in this reply for your reference. You may
download it with Outlook Express, not IE.

Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights

Jun 21 '06 #2
Hi George,

Thanks for your feedback information!

Yes, I think your background information is critical regarding resolving
your problem.

FileSystemWatch er event fires in another thread other than the main GUI
thread, if you add the new row into the DataTable directly, the .Net
Winform databinding code will try to update the new row into the
DataGridView UI.
However, .Net Windows Forms uses the single-threaded apartment (STA) model
because Windows Forms is based on native Win32 windows that are inherently
apartment-threaded. The STA model implies that a window can be created on
any thread, but it cannot switch threads once created, and all function
calls to it must occur on its creation thread. So your code indirectly
manipulates the DataGridView from the non GUI thread which offends the STA
rule in .Net Winform. This may cause some strange and hard to detect
multithreading issue. Please refer to the the article below for more
information:
"Multithrea ded Windows Forms Control Sample"
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp

You should use Control.Invoke method to marshal the manipulating to the GUI
control from another thread. Below is my modified sample code snippet:

DataTable dt;
private void Form1_Load(obje ct sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add( "column1", typeof(int));
dt.Columns.Add( "column2", typeof(string)) ;

for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr) ;
}
this.dataGridVi ew1.DataSource = dt;

FileSystemWatch er fsw = new FileSystemWatch er("C:\\");
fsw.EnableRaisi ngEvents = true;
fsw.Created += new FileSystemEvent Handler(fsw_Cre ated);
}

void fsw_Created(obj ect sender, FileSystemEvent Args e)
{
this.dataGridVi ew1.Invoke(new EventHandler(bu tton1_Click));
}

private void button1_Click(o bject sender, EventArgs e)
{
DataRow dr=this.dt.NewR ow();
dr["column1"] = 1000;
dr["column2"] = "NewItem";
dt.Rows.Add(dr) ;
}

This code snippet works well on my side. If I create a new file in the
"C:\" folder, a new row will be added to the DataGridView. I have also
attached the modified project in this reply for your reference.

I hope this will resolve your problem. If you still have any problem of
resolving it, please feel free to tell me, I will work with you.

Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 22 '06 #3
Hi Jeffrey,

Thanks, your suggestion works. I no longer needs to manually refresh or
ResetBindings the DataGridView.

I have some additional questions/comments though. Questions first.

Q. Should I wrap all readonly, read-write and write calls to DataTable
(bounded source) and use Control.Invoke to ensure UI thread is used? For
example, DataTable.Rows[index] could be read only, or write.
DataTable.Updat e() may cause changes. How far should I go?

Comment: Wrapping these calls become very cumbersome, and potentially a
bottle neck since UI thread may be bogged down by work. Does Microsoft plan
to make Data bound control such as DataGridView, multi-thread safe?

Thanks,
--
George
""Jeffrey Tan[MSFT]"" wrote:
Sorry, forgot to attach the sample project. Here it is.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights

Jun 22 '06 #4
Jeffrey,

DataTable.Updat e(), I meant DataAdapter.Upd ate(DataTable). This potentially
changes DataTable.

Thanks,
--
George
"George" wrote:
Hi Jeffrey,

Thanks, your suggestion works. I no longer needs to manually refresh or
ResetBindings the DataGridView.

I have some additional questions/comments though. Questions first.

Q. Should I wrap all readonly, read-write and write calls to DataTable
(bounded source) and use Control.Invoke to ensure UI thread is used? For
example, DataTable.Rows[index] could be read only, or write.
DataTable.Updat e() may cause changes. How far should I go?

Comment: Wrapping these calls become very cumbersome, and potentially a
bottle neck since UI thread may be bogged down by work. Does Microsoft plan
to make Data bound control such as DataGridView, multi-thread safe?

Thanks,
--
George
""Jeffrey Tan[MSFT]"" wrote:
Sorry, forgot to attach the sample project. Here it is.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights

Jun 22 '06 #5
Hi George,

Thanks for your feedback!

1. Yes, read-only manipulation to the GUI thread also needs to be wrapped
with Control.Invoke/BeginInvoke method. Let's suppose a control is still
updating its state while you are retrieving its state, you may get an
immediate partial updated state which may appear to be incorrect.

2. To wrap these methods or properties calling, you may write a
method(let's call it WrapMethod() ) in Form class which contains all these
code, then in FileSystemWatch er thread, you can just marshal once calling
to the WrapMethod(), not marshal to all the code statements. This will save
a lot of work.

3. Yes, any operations may cause calling to the GUI thread
methods/properties need to be mashaled.

4. Yes, I see your concern, many customers have feedbacked such request
regarding .Net winform multithreading. However, this marshaling request is
not caused by .Net Winform code, it is caused by the native Win32 standard
controls. Native Win32 windows that are inherently apartment-threaded
without thread-safe build-in for legacy reason. Because .Net controls
always encapsulate the native Win32 standard controls, it is almost
impossible for .Net winform to get rid of the STA feature. Thanks for your
understanding.

Hope my comment makes sense to you.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 23 '06 #6
Hi George,

Does my reply make sense to you? If you have any concern, please feel free
to tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3150
by: RSH | last post by:
Hi, I have a situation where I have a DDL that lists all of the tables in a particular database, and a Datagridview which appears in the same from below the DDL.. When the user selects a new database table from the list I refresh the datagridview with the newly selected tables data. This works fine but after a few tables have been selected it takes a very long time to load some of the tables. Some tables that contain only one row of...
2
5019
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and column A binds to string A and column B binds to int B. When i start two threads, the datagridview able to show two rows and updated int B at run time. How? public class Foo { public Foo()
0
1265
by: rodchar | last post by:
hey all, i have a 1 object datasource, formview and gridview using that datasource. when i add a new record via formview how can i automatically refresh the gridview? note: the object datasource is bound to a custom object. thanks, rodchar
3
7594
by: David Cartwright | last post by:
Hi all, I'm having a weird time with a call to the Refresh() method of a DataGridView. I have a VB.NET 2005 Windows application with a main form and a "worker" thread. The main form delegates a Sub to the worker so that the latter can refresh the DataGrid after updating the DataTable driving it. When I run the app under the debugger, everything's fine. But when I run it for real, it hangs, apparently in the Refresh() method of the...
3
27947
by: M K | last post by:
I have added a record to the underlying db and after I add the record i do a datagridview.Refresh(); and i dont see the newly added record. If i stop and start the application its there. What am I doing or not doing? Mark
7
7388
by: mvenkatesan | last post by:
Hi guys I am win appl. I was bind the datagridview from datatable. i was changed datas @ runtime. datatable changed but my datagridview not refresh. I written dagaridview.refresh(); not work how to refresh? Thanks in advance Venky
0
1233
by: aboobackerpm | last post by:
i am using a binded dgv with datatable my problem is how i can update the changes in a cell of a dgv to datatable without move to next cell my dgv's allow addnew property is false because of that if user is in last row he cannot go to next row after changes, without go to next row dgv data does not updating the datatable. Thanks in Advance
0
1136
by: kjqua | last post by:
Hi every one. I will like to know if is possible to copy data from a datagridview to a datatable. If is possible how can i do it. The datagrigview is not connect to a source, i add manually data to the datagridview. thanks marco
1
3759
by: Aegixx | last post by:
Ok, extremely wierd situation here: (I'll post the code below, after the explanation) I've got a Windows application (.NET 3.5) that has a single Form with a DataGridView embedded. The user presses a button to do a SQL query (fill a DataSet). Before firing the query, I disable the grid/buttons and turn on UseWaitCursor. After it completes, I re-enable the grid/ buttons, refresh the dataset, then turn off the UseWaitCursor. If I...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.