Introduction
I have a datagridview that has its data populated by assigning a datatable to it early on in my code. The data shows up no problem. I am then trying to later go through and apply a back color and font to only certain cells so not all of them.
What I have tried
Now from doing some research this seems to be done through the styles property of each cell. I have built a loop like so:
Dim strikethrough_style As New DataGridViewCellStyle
strikethrough_style.Font = strikethrough_font
strikethrough_style.BackColor = Color.Red
For row = 0 To DataGridView1.Rows.Count - 1
For col = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Rows(row).Cells(col).Style = strikethrough_style
Next
Next
This should supposedly change all of my cells to have a red background and have a strikethrough font however none of the cells change when ran.
I can change the defaultcellstyle property like this:
DataGridView1.DefaultCellStyle = strikethrough_style
which does work for all cells,
but like I said above, I want to ultimately only change certain rows, not all. Is there something overriding my style? If so, how can I get around this?
Update
So oddly enough if I try:
DataGridView1.Columns(4).DefaultCellStyle = strikethrough_style
the column does apply the style; however, when I try:
DataGridView1.Rows(1).DefaultCellStyle = strikethrough_style
the row does not. Seems odd that I can apply a style to a column and not a row.
Best Answer
So I found the answer after trying many things and wanted to share so nobody makes the same mistake that I did. Initially I had my styling code occuring right after my datagridview was bound:
The problem with this is that the datagridview is taking too long to finish binding the data.
The better approach is to do the styling after the data has been bound through the bind complete event: