Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Right way to use a DataGridView Bound to Data
 

Right way to use a DataGridView Bound to Data

Hi there this question is fairly noobish but bear with me.

What would be the right way to add Data to DataGridViewCells in a Databound DataGrid.

Here is the complete problem:

I have a DataBound Datagrid but the DataSet/DataTable behind doesnt have all the required data so I used the dataBoundComplete Event to add my data based on what the dataGrid has.

the data added are some images, some values and colors based on what is on the datagridview i change the color of the rows or the text in the cells. I add an index to the first cell of every row to number every row. Then I change the color of that cell to a dark grey, after that I color the entire row the even rows are light grey the odd rows are white. After that I add some required data to the DataGridViewCells depending on some other objects all of them are in memory and asociated to the rows... Some of this data are Images displayed only if needed.

This seemed to work but its VERY slow when using more than 30 records... What is going on?

What alternatives do I have?

I dont know what to do because the performance of the application degrades a lot when using this aproach.

Ed_Zero  Friday, November 17, 2006 2:55 AM

I think you should add data into datatable rather than directly into datagridviewcell.

The Low performance may be caused by adding index in the first column, because datagridview will be refreshed everytime you add a new data.

Wang Chi  Tuesday, November 21, 2006 9:48 AM

i found out a workaround to this...

Indeed adding data to the datagridview directly is not so good but sometimes you cant help it... for example images or calculated items...

The problem is in the lifecycle of the component when I use DataBindingComplete event its too late and the component is refreshing itself to reflect changes.

Instead I used the CellFormating event this is called before the component is validated and refreshed. The performance improved impresively.

http://msdn2.microsoft.com/en-us/library/ms171622.aspx

That page helped a lot too.

Ed_Zero  Tuesday, November 21, 2006 5:49 PM

I think you should add data into datatable rather than directly into datagridviewcell.

The Low performance may be caused by adding index in the first column, because datagridview will be refreshed everytime you add a new data.

Wang Chi  Tuesday, November 21, 2006 9:48 AM

i found out a workaround to this...

Indeed adding data to the datagridview directly is not so good but sometimes you cant help it... for example images or calculated items...

The problem is in the lifecycle of the component when I use DataBindingComplete event its too late and the component is refreshing itself to reflect changes.

Instead I used the CellFormating event this is called before the component is validated and refreshed. The performance improved impresively.

http://msdn2.microsoft.com/en-us/library/ms171622.aspx

That page helped a lot too.

Ed_Zero  Tuesday, November 21, 2006 5:49 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView displaying DataSet from Web Service
• Binding.WriteValue() does not work
• DataGridView, uncontrolled adding rows
• Column Resizing question on DataGridView
• Validating using DataTable events vs DataGridView events
• add a data for every record
• format a column into a datagrid using a typed collection
• Combo Box inside DataGridView using DataTable
• Bindingsource not in sync
• Where do I put custom code for adding DateTimePicker to DataGridView??