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.
IsThreeState="True" Binding=" {Binding OnlineOrderFlag}" /> </DataGrid.Columns> </DataGrid> Use DataGridCheckBoxColumn to display columns of Boolean data as a CheckBox. The following illustration shows an example of DataGridCheckBoxColumn. To populate the column, bind the column to the data by using the Binding property.
You can customize the DataGridCheckBoxColumn by setting properties, such as Width, Header, IsThreeState, and IsReadOnly. If you want to display other types of data, DataGrid provides the following column types: Use to display URI data. Use to display enumeration data. Use to display text.
The Binding property is applied to the CheckBox element created in the column. The DataContext for the element in each cell is the data item for the row the cell is in. Therefore, to set up the binding you only have to set the Binding.Path. Optionally, you can specify a Binding.Converter if your data source and target are different types.
Gets a read-only CheckBox control that is bound to the column's Binding property value. Retrieves the Content property value for the cell at the intersection of this column and the specified row. Gets the Content property value for the cell at the intersection of this column and the row that represents the specified data item.
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;
}
}
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);
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With