Greyout checkbox column by clicking on another checkbox column in a DataGridView

Niladri.Biswas
Posted by in Windows Forms category on for Beginner level | Points: 250 | Views : 4925 red flag

In this article, we will see as how to greyout checkbox column by clicking on another checkbox column in a DataGridView.


 Download source code for Greyout checkbox column by clicking on another checkbox column in a DataGridView

Introduction

In this article, we will see as how to greyout checkbox column by clicking on another checkbox column in a DataGridView.

Straight to Experiment

Let us create a "Windows Forms Application" and add a "DataGridView" as under

Next in the code behind create a class as under

public class FileInformation
{
	public bool IsWarning { set; get; }
	public bool Enabled { set; get; }
}

This class holds the information about "IsWarning" and the "Enabled" link.

Next let us write the code that will give a good look and fell to our grid

private void PrepareGrid()
{
	var checkBoxErrorColumn = new DataGridViewCheckBoxColumn
	{
		Name = @"dgvcheckBoxErrorColumn",
		HeaderText = "FirstCheckboxColumn",
		DataPropertyName = @"IsWarning",
		AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells,
		ReadOnly = false,
		Frozen = false,               
		SortMode = DataGridViewColumnSortMode.Programmatic
	};
	dataGridView1.Columns.Add(checkBoxErrorColumn);


	var checkBoxEnableColumn = new DataGridViewCheckBoxColumn
	{
		Name = @"dgvcheckBoxEnableColumn",
		HeaderText = "SecondCheckboxColumn",
		DataPropertyName = @"Enabled",
		AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells,
		ReadOnly = false,
		Frozen = false,
		SortMode = DataGridViewColumnSortMode.Programmatic
	};
	dataGridView1.Columns.Add(checkBoxEnableColumn);
	
	this.dataGridView1.AllowUserToAddRows = false;
	this.dataGridView1.AllowUserToDeleteRows = false;
	this.dataGridView1.AllowUserToResizeRows = false;
	this.dataGridView1.RowHeadersVisible = false;
	this.dataGridView1.MultiSelect = false;
	this.dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
	this.dataGridView1.AutoGenerateColumns = false;
}

In the "PrepareGrid()" method, we are giving mainly a Name, HeaderText and DataPropertyName to the columns that should participate in the DataGridViewControl.

Once done, the next task is to populate the grid content

private void DisplayResult()
{
	FileInformationList = LoadItems();
	dataGridView1.DataSource = FileInformationList;
}

private List<FileInformation> LoadItems()
{
	var lstScriptInfo = new List<FileInformation>();
	for (int i = 1; i <= 5; i++)
	{
		lstScriptInfo.Add(new FileInformation { IsWarning = true, Enabled = true });
	}
	return lstScriptInfo;
}

The code is preety straight forward.We are preparing a list of 5 items and binding the same to the grid

The objective is that, if the checkbox of the "FirstCheckboxColumn" is unchecked, then the corresponding "SecondCheckboxColumn" will be unchecked and it will be greyedout.

First write the below code in the dataGridView1.CellContentClick event

void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
	if (e.RowIndex > -1 & e.ColumnIndex == 0)
	{
		var editingCellFormattedValue = Convert.ToBoolean(((DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0]).EditingCellFormattedValue);

		if (editingCellFormattedValue == false)
		{
			dataGridView1[1, e.RowIndex].Value = false;
			dataGridView1[1, e.RowIndex].ReadOnly = true;

		}
		else
		{
			dataGridView1[1, e.RowIndex].Value = true;
			dataGridView1[1, e.RowIndex].ReadOnly = false;
		}
	}
}

This code will make the "SecondCheckboxColumn" readonly once the "FirstCheckboxColumn" is unchecked.

Finally write the below code in the "CellPainting" event

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
	if (e.ColumnIndex > -1 &&
		e.RowIndex > -1 &&
		dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
		dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
	{
		Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal);
		e.Handled = true;
		e.PaintBackground(e.CellBounds, true);
		if (e.Value != null)
		{
			CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.CellBounds.X + e.CellBounds.Width / 2 - checkSize.Width / 2, e.CellBounds.Y + e.CellBounds.Height / 2 - checkSize.Height / 2),
		   (bool)e.Value ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled);
		}
	}
}

Conclusion

In this article we have seen how to greyout checkbox column by clicking on another checkbox column in a DataGridView.Hope this will be helpful.Thanks for reading.Zipped file is attached

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)