datagridview .net 2.0

G

Guest

hi,
ive a datagridview cotrol on my winform..
i want to show a contextmenustrip when i right click a cell or row of my
grid...
how can i acheive it?
 
C

ClayB

One way you can do this is to handle the CellMouseUp event.

void dataGridView1_CellMouseUp(object sender,
DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
DataGridView grid = sender as DataGridView;
ContextMenuStrip menu = new ContextMenuStrip();
menu.Items.Add("Task1", null, new
EventHandler(Task1_Click));
menu.Items.Add("Task2", null, new
EventHandler(Task2_Click));
Point pt = grid.PointToClient(Control.MousePosition);
menu.Show(dataGridView1, pt.X, pt.Y);
}
}

====================
Clay Burch
Syncfusion, Inc.
 
L

Larry Smith

hi,
ive a datagridview cotrol on my winform..
i want to show a contextmenustrip when i right click a cell or row of my
grid...
how can i acheive it?

See properties "DataGridView.ContextMenuStrip",
"DataGridViewColumn.ContextMenuStrip" and
"DataGridViewRow.ContextMenuStrip". Note that you can usually handle things
using the forms designer. Drag a "ContextMenuStrip" from the tools box onto
your form. You can then select it from the grid's "ContextMenuStrip"
property (it appears in the dropdown list). Add items to it accordingly
using the designer. Note that the menu will apply to entire grid in this
case. You can set the same property on individual rows and columns however
(well, columns anyway - don't recall if the designer provides row support)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top