Reputation: 6061
I need to be able to turn off some cells in a row based on a boolean flag. If the flag is true I everything should be enabled and visible like normal. If the flag is false however I need to have several cells in the row made invisible and readonly.
Upvotes: 1
Views: 1914
Reputation: 20818
Here is my example. Add an event handler for CellPainting, then determine if the item is disabled or not. If it is disabled, then just paint the background and make the cell read-only.
I've got a custom class BoardStatusView bound to the data grid, which has a boolean function that determines if the cell should have a check box or not (Upgradeable()
)
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0)
{
BoardStatusView bs = dataGridView1.Rows[e.RowIndex].DataBoundItem as BoardStatusView;
bool disabled = !bs.Upgradeable();
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = disabled;
if (disabled && e.ColumnIndex == 0)
{
e.PaintBackground(e.ClipBounds, false);
e.Handled = true;
}
}
}
Upvotes: 0
Reputation: 6450
You can handle the CellPainting event, check the status of your flag there and then paint the cell to be shown/hidden.
This link on MSDN may help you in this:
http://msdn.microsoft.com/en-us/library/hta8z9sz.aspx
Upvotes: 1