Notifications
Clear all

[Closed] Cell Style in DataGridView problem

hi all!
I have a weird problem setting the backcolor in my datagridview.

Each time i execute a script, it takes a little more time each time ( time = timeInFirstExecution * numberOfExecutions).

code sample with the problem:

(	
  	 global dataGridViewRol
  	 try (destroydialog dataGridViewRol)
  	 catch()
  	 
  	 rollout dataGridViewRol "DataGridView"
  	 (
  		 dotnetcontrol dgv "DataGridView"  width:420 height:220 align:#center
  		 
  		 on dataGridViewRol open do
  		 (
  			 dgv.Dock = dgv.Dock.Fill
  			dgv.RowHeadersVisible = false 			
  			for i = 1 to 8 do 
  			(
  				if (mod i 2 as integer != 0) then 
  					dgv.columns.add (dotnetobject "DataGridViewTextBoxColumn")
  				else
  					dgv.columns.add (dotnetobject "DataGridViewCheckBoxColumn")
  			)
  			
  			for i=0 to dgv.Columns.count-1 do dgv.Columns.Item[i].width = 50
  			numCells = dgv.Columns.count - 1
  			oddColor = (dotNetClass "System.Drawing.Color").Gray
  			evenColor = (dotNetClass "System.Drawing.Color").LightGray
  			
  			dtStartTime = (dotnetclass "system.datetime").Now
  			 for i = 1 to 20 do
  			 (
  				odd = (mod i 2 as integer != 0)
  				 row = dotnetobject "DataGridViewRow"
  				 row.DefaultCellStyle = dgv.RowsDefaultCellStyle		
  				 dgv.rows.add row
  				rowCells = row.cells
  				 rowCells.item[0].value = "Column1"
  				rowCells.item[1].value = not odd
  				rowCells.item[2].value = "Column3"
  				rowCells.item[3].value = odd
  				rowCells.item[4].value = "Column5"
  				rowCells.item[5].value = not odd
  				rowCells.item[6].value = "Column7"
  				rowCells.item[7].value = odd
  				/* uncomment the next two lines for a very slow loop, and slower each execution
  				for j = 0 to numCells do
  					rowCells.Item[j].Style.BackColor = (if odd then oddColor else evenColor)
  				--*/
  			 )		
  			dtFinishTime = (dotnetclass "system.datetime").Now
  			sScriptTime = (dtFinishTime.subtract dtStartTime).tostring()
  			format "Set Rows Execution Time: % 
" sScriptTime
  		 )
  	 )
  	 createdialog dataGridViewRol 420 225
   )
14 Replies

everything is OK. don’t worry. sometimes it creates(loads) slower, sometimes faster. it probably depends on .net garbage collection.

Thanks for the reply denis!
I think now I can sleep well.

I can´t wait seconds if i load a lot of info for a lot of objects. So, i will change only the first cell backcolor for clarity.

sorry but you can’t. i see a problem… something is definitely wrong.

Yes! i am not crazy!

But i didn’t found exactly where is the problem or how to solve it. I tried some ideas but without success.
For sure i am doing something wrong, or not as efficient as it can be done.

Thanks for think on it!

if you want to have just only different backcolors (or whole style) for even and odd row use AlternatingRowsDefaultCellStyle ( http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.alternatingrowsdefaultcellstyle%28v=vs.110%29.aspx )

but if you want to color every cell… hmm… yes. something is not disposing. system still keeps copies of all created cells. so every time you create new set, but system doesn’t dispose old. and it has to update all instances. why it happens i don’t know yet

here is a workaround:

			  oddColor = (dotNetClass "System.Drawing.Color").Gray
			  evenColor = (dotNetClass "System.Drawing.Color").LightGray

			oddStyle = dotnetobject "DataGridViewCellStyle"
			oddStyle.ApplyStyle dgv.RowsDefaultCellStyle
			oddStyle.Backcolor = oddColor 
			evenStyle = dotnetobject "DataGridViewCellStyle"	
			evenStyle.ApplyStyle dgv.RowsDefaultCellStyle
			evenStyle.Backcolor = evenColor 
			
			--format ">> % %
" (oddStyle.Backcolor.ToString()) (evenStyle.Backcolor.ToString())
			
			  dtStartTime = (dotnetclass "system.datetime").Now
			   for i = 1 to 20 do
			   (
				  odd = (mod i 2 as integer != 0)
				row = dotnetobject "DataGridViewRow"
				--row.DefaultCellStyle = if odd then oddStyle else evenStyle
				dgv.rows.add row
				  rowCells = row.cells
				  --uncomment the next two lines for a very slow loop, and slower each execution
				  for j = 0 to numCells do rowCells.Item[j].Style = (if odd then oddStyle else evenStyle)
				  --*/
			   )   

but i still don’t understand what happens in your original solution…

but as i said, you only need


			dgv.AlternatingRowsDefaultCellStyle.Backcolor = evenColor

btw… row index starts from 0, so you have change logic what is odd, and what is even to opposite

Sadly no.
I use the backcolor as additional visual help to separate related objects in the datagridview.

I remember AlternatingRowsDefaultCellStyle after wrote the sample code, it’s a regular sample code to see the exact problem.

I don’t use the script many times on the same max sesion, so i can avoid a serious problem.

Thanks for the time!

I didn’t see your last reply, i will try it!

Very thanks!

Page 1 / 2