I have a DataGridView filled with productinformation. The datagridview has totally 50 columns but the users don't always need all the columns, I want to help them to be able to choose which columns to show and which ones not to show.
One solution that I would like to programm is that when the user right clicks on the columns they can choose from a list that pops up choose which columns to show and which ones not to shos. Just like the image below.
How can I do that. I would really appreciate any help.
You can achieve this using the WinForms ContextMenuStrip and the Visible property of DataGridView columns.
Here is some example code that does what you want:
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindingList<User> users = new BindingList<User>{
new User{Name = "John", Address="Home Street", Title="Mr."},
new User{Name = "Sally", Address="Home Street", Title="Mrs."}
};
contextMenuStrip1.AutoClose = true;
contextMenuStrip1.Closing += new ToolStripDropDownClosingEventHandler(contextMenuStrip1_Closing);
dataGridView1.DataSource = users;
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
}
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewColumn gridViewColumn in this.dataGridView1.Columns)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Name = gridViewColumn.Name;
item.Text = gridViewColumn.Name;
item.Checked = true;
item.CheckOnClick = true;
item.CheckedChanged += new EventHandler(item_CheckedChanged);
contextMenuStrip1.Items.Add(item);
}
foreach (DataGridViewColumn gridViewColumn in this.dataGridView1.Columns)
{
gridViewColumn.HeaderCell.ContextMenuStrip = contextMenuStrip1;
}
}
void item_CheckedChanged(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
if (item != null)
{
dataGridView1.Columns[item.Name].Visible = item.Checked;
}
}
void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
{
e.Cancel = true;
}
}
}
public class User
{
public string Name { get; set; }
public string Address { get; set; }
public string Title { get; set; }
}
}
The User class there is just so the example compiles, providing something to bind my DataGridView to.
I've also added some code that allows users to click more than one column at a time (by checking the close reason on closing and cancelling if it was an item select). This is actually a little borderline in terms of diverting from standard UI behaviour in my opinion - it is usually better to stick with standard behaviour, but I included since it is (I think) useful in this scenario.
Also, it is generally tidier to put this sort of customisation into a new control that inherits from DataGridView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With