views:

47

answers:

3

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?

A: 

I believe you could accomplish this just by setting the particular columns visible flag to false.

pstrjds
I know this trick, by it must looks ok for user, so I think about checkbox at column header(when he click at it, columns would dissapear) but I don'k know if it is the best solution
You could add a menu with toggled check menu items that would show and hide, you could also enhance to have both a menu item and a context menu. If you have several columns I think the check box on the form itself would be too much from a UI perspective. If you think of the windows task manager, the show/hide columns is in a menu.
pstrjds
A: 

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.

adam
it's quite good, but there should be one checkbox per column and I don't say that these idea with checkbox is the best
true, there are different ways to go about it; basically link some type of event (an onclick, checkchanged, whatever) to hide the columns; just giving a sample code here - without seeing any of the actual code, I can't really suggest anything more specific to a certain need
adam
A: 

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!

Homam