Reputation: 21
How Can I Visible and Invisible Columns in DataGridView By Checked and Unchecked Check box's using WinForm, C#.
Upvotes: 1
Views: 354
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
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