Notifications
Clear all

[Closed] dotnet datagridview defaultcellstyle error

I’m trying to format the rows of my dgv using :
dgv.rows.item[x].defaultcellstyle.backcolor = dgv.rows.item[x].DefaultCellStyle.backcolor.fromARGB 66 66 66

For some reason, when this is called on multiple rows, I get the error :
dotNet runtime exception: Item has already been added. Key in dictionary: ‘DataGridViewCellStyle { BackColor=Color [A=255, R=66, G=66, B=66] }’ Key being added: ‘DataGridViewCellStyle { BackColor=Color [A=255, R=66, G=66, B=66] }’

Can anyone explain how I’m supposed to set default cell styles on a row-by-row basis in dotnet on a datagridview? I’m stumped !

Thanks
p.

1 Reply

Aha, I should have searched more carefully…
This thread provides a working example : http://forums.cgsociety.org/archive/index.php/t-1141988.html

I needed to create a style and apply that, rather than adjust the defaultcellstyle for each row.

– define the style
selStyle = dotnetobject “DataGridViewCellStyle”
selStyle.ApplyStyle rssn_ui.dgvmain.RowsDefaultCellStyle
selStyle.Backcolor = theselectedCol

– set the row cellstyle
row.defaultcellstyle=selStyle