Reputation: 3792
I have a winForm with a DataGridView control. It contains 5 columns, one of them is a CheckBox column. I want to enable/disable checkbox cell of this column based on the value present in another column at the same row.
I can disable entire column using DisabledCheckBoxCell
But it makes entire column in disabled state.
Here is a snippet of DataGridView,
SourceColumn | DestinationColumn
true | enabled
true | enabled
false | disabled
Does anyone have idea, how this can be achieved in .Net.
Upvotes: 13
Views: 42691
Reputation: 9
foreach (DataGridViewRow row in dataGridView1.Rows)
{
var cell = dataGridView1[cellindex, row.Index];
if (cell.Value != null )
if((bool)cell.Value == true)
{
....
}
}
Upvotes: -1
Reputation: 2639
I ended up doing something like this to get an actual disabled checkbox to show up:
using System.Windows.Forms.VisualStyles;
public partial class YourForm : Form
{
private static readonly VisualStyleRenderer DisabledCheckBoxRenderer;
static YourForm()
{
DisabledCheckBoxRenderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedDisabled);
}
private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex > -1)
{
int checkBoxColumnIndex = this.yourCheckBoxColumn.Index;
var checkCell = (DataGridViewCheckBoxCell)this.dataGridView[checkBoxColumnIndex, e.RowIndex];
var bounds = this.dataGridView.GetCellDisplayRectangle(checkBoxColumnIndex , e.RowIndex, false);
// i was drawing a disabled checkbox if i had set the cell to read only
if (checkCell.ReadOnly)
{
const int CheckBoxWidth = 16;
const int CheckBoxHeight = 16;
// not taking into consideration any cell style paddings
bounds.X += (bounds.Width - CheckBoxWidth) / 2;
bounds.Y += (bounds.Height - CheckBoxHeight) / 2;
bounds.Width = CheckBoxWidth;
bounds.Height = CheckBoxHeight;
if (VisualStyleRenderer.IsSupported)
{
// the typical way the checkbox will be drawn
DisabledCheckBoxRenderer.DrawBackground(e.Graphics, bounds);
}
else
{
// this method is only drawn if the visual styles of the application
// are turned off (this is for full support)
ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Inactive);
}
}
}
}
}
Upvotes: 3
Reputation: 800
Vijay,
DataGridViewCheckBoxColumn does not have property called disabled so by changing the style of the checkbox you can make it look like as if it is disabled. Look at the following code.
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == 1)
{
DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0];
DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
chkCell.Value = false;
chkCell.FlatStyle = FlatStyle.Flat;
chkCell.Style.ForeColor = Color.DarkGray;
cell.ReadOnly = true;
}
}
Upvotes: 26
Reputation: 4652
Try using RowDataBound event for your GridView. You can cast eventargs to row, find control on this row and disable it. This event fires for each row of gridview, even for headers and footers, so try not to catch exception. Here is some code might be useful to you
protected void xxx_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row != null) && e.Row.RowType == DataControlRowType.DataRow)
Upvotes: 0