I have a datagridview in my application. I want to be able to select one or more rows, then right mouse click and get a context menu. Options in the context menu will do something with the selected rows, like hide them for example. For the datagridview, I have multiselect=true and selectionmode=fullrowselect. For some reason, the datagridview.selectedrows.count is always = 0. Why might this be? Here is code...
private void Form1_Load(object sender, EventArgs e)
{
Image img = null;
contextMenuStrip1.Items.Add("Hide selected", img, new System.EventHandler(contextMenuStrip1_Click));
contextMenuStrip1.Items.Add("Unhide all", img, new System.EventHandler(contextMenuStrip1_Click));
dataGridView1.ContextMenuStrip = contextMenuStrip1;
}
private void contextMenuStrip1_Click(object sender, EventArgs e)
{
switch (sender.ToString().Trim())
{
case "Hide selected":
//Necessary because a row with the current cell cannot be hidden.
dataGridView1.CurrentCell = null;
int count = dataGridView1.SelectedRows.Count;
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
row.Visible = false;
}
break;
case "Unhide all":
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Visible = true;
}
break;
}
}