sarmand mahmood
sarmand mahmood

Reputation: 21

How To Visible DataGridView Columns Using Checkbox's C#

How Can I Visible and Invisible Columns in DataGridView By Checked and Unchecked Check box's using WinForm, C#.

enter image description here

Upvotes: 1

Views: 354

Answers (2)

Jimmy
Jimmy

Reputation: 1072

In case of Windows Forms:

You can specifiy the visibility for each DataGridViewColumn by means of the DataGridViewColumn.Visible property, for example:

dataGridView1.Columns["nameOfColumnThatShouldBeInvisible"].Visible = checkBox1.Checked;

Also, you might want to evoke a rebind when the checkboxes that determine whether certain columns are displayed or not, get checked/unchecked:

checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

public void checkBox1_CheckedChanged(Object sender, EventArgs e) 
{
    dataGridView1.Columns["nameOfColumnThatShouldBeInvisible"].Visible = checkBox1.Checked;
    //rebind dataGridView1 so as to show/hide column for clicked checkbox
}

Upvotes: 2

Smolakian
Smolakian

Reputation: 414

For each of the Datagrid columns bind the Visibility property of the column to the IsChecked property of the appropriate checkbox.

Set the converter in the binding to use BooleanToVisibilityConverter.

(This solution assumes that you are using WPF...)

Upvotes: 2

Related Questions