Reputation: 1569
I am trying to format specified rows in DataGridView, but it keeps formatting all the rows in my DataGridView. This is what I am doing:
private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DeParti tmp in bnsParti)
{
if (tmp.Arti.Type == ArtiType.Fast)
{
if (e.ColumnIndex == 0 || e.ColumnIndex == 3 ||
e.ColumnIndex == 8 || e.ColumnIndex == 9)
{
e.Value = "";
}
}
}
}
Using this type of code it keeps setting cell value to " ", in all rows, but I only want that the value is " ", when the Arti type is Fast. Any ideas.
Thanks in advance.
Upvotes: 1
Views: 1638
Reputation: 4695
If you have to format specified rows, why are you checking the columns??
Access the DataBoundItem (the object associated with the row being formatted) and modify the Value according to your logic. Do not access the binding source directly. You code should be
private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((Rows[e.RowIndex].DataBoundItem as DeParti).Arti.Type == ArtiType.Fast)
{
e.Value = "";
}
}
This will "clean" all the cells in the row, you can add a check if you wish to set the Value="" only on some Columns, for example
private void dgwParti_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((Rows[e.RowIndex].DataBoundItem as DeParti).Arti.Type == ArtiType.Fast
&& e.ColumnIndex == 8)
{
e.Value = "";
}
}
Upvotes: 2