Adding items to DataGrid in runtime

I have a system.windows.forms.form with a datagrid in it. in the load method i bind a dataTable into it. and i have a method that in runtime adds some items to the dataTable after an event. i did the following: DataTable dataTable =(DataTable)dataGrid.dataSourse; DataRow dataRow = dataTable.newRow(); dataRow[0] = "some string"; dataRow[1] = "another string"; dataTable.Rows.Add(dataRow); it works, but when there are more than 10 items (the max display without a scrollBar), the scrollBar is added. sometimes it''s stuck, and if not, when adding a new item there are two scrollBars, and other freak things. When adding items "slowly" it doesn''t always do that, it happens when adding 4-5 items one after another. Thanks in advance, Hisham

2 Replies

HI Hisham August 24, 2005 07:10 AM UTC

I got the solution (and i feel very stupid). The dataGrid updates came from different threads, that''s the reason for the problem. I solved it using BeginInvoke. Thanks, Hisham >I have a system.windows.forms.form with a datagrid in it. in the load method i bind a dataTable into it. >and i have a method that in runtime adds some items to the dataTable after an event. i did the following: > >DataTable dataTable =(DataTable)dataGrid.dataSourse; >DataRow dataRow = dataTable.newRow(); >dataRow[0] = "some string"; >dataRow[1] = "another string"; >dataTable.Rows.Add(dataRow); > >it works, but when there are more than 10 items (the max display without a scrollBar), the scrollBar is added. >sometimes it''s stuck, and if not, when adding a new item there are two scrollBars, and other freak things. >When adding items "slowly" it doesn''t always do that, it happens when adding 4-5 items one after another. > > >Thanks in advance, >Hisham >


PE Pet December 6, 2005 03:20 AM UTC

Hi, I am facing similar problme. when i bind the datatable to datagrid i am getting nullreferrence error because of multi threading. How you implement BeginInvoke? can you provide me sample for implementing BeginInvoke in DataTable

Loader.
Up arrow icon