Reputation: 20242
I'm binding dataset to datagridview, and I want give user possibility to removing(maybe by checkbox ?) columns which he doesn't know to see.
So, at start he see 5 columns, and he wants look only at three, so he clicks at something and these columns dissapear.
What are you using to give user this functionality?
Upvotes: 1
Views: 4489
Reputation: 23851
I suggest the following:
Create a checkedListBox and add to it a CheckBox Item for each column in the grid, this is the code:
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
checkedListBox1.Items.Add(column.HeaderText, column.Visible);
checkedListBox1.ItemCheck += (ss, ee) =>
{
if (checkedListBox1.SelectedItem != null)
{
var selectedItem = checkedListBox1.SelectedItem.ToString();
dataGridView1.Columns[selectedItem].Visible = ee.NewValue == CheckState.Checked;
}
};
}
Good luck!
Upvotes: 1
Reputation: 1606
If you used a checkbox, you would have something like this:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
if (c.Checked)
Column1.Visible = true;
else
Column1.Visible = false;
}
You would just modify the Column1 name to be whatever column you want to show/hide and link the event to the proper checkbox(es).
In the Constructor for the Form I would do something like Checkbox1.checked = true;
so the first _CheckChanged would hide it, but that is up to you.
Upvotes: 2
Reputation: 17428
I believe you could accomplish this just by setting the particular columns visible flag to false.
Upvotes: 1