I have a datagridview in a .NET winform app. I would like to rightclick on a row and have a menu pop up. Then i would like to select things such as copy, validate, etc
How do i make A) a menu pop up B) find which row was right clicked. I know i could use selectedIndex but i should be able to right click without changing what is selected? right now i could use selected index but if there is a way to get the data without changing what is selected then that would be useful.
You can use the CellMouseEnter and CellMouseLeave to track the row number that the mouse is currently hovering over.
Then use a ContextMenu object to display you popup menu, customised for the current row.
Here's a quick and dirty example of what I mean...
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu m = new ContextMenu();
m.MenuItems.Add(new MenuItem("Cut"));
m.MenuItems.Add(new MenuItem("Copy"));
m.MenuItems.Add(new MenuItem("Paste"));
int currentMouseOverRow = dataGridView1.HitTest(e.X,e.Y).RowIndex;
if (currentMouseOverRow >= 0)
{
m.MenuItems.Add(new MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
}
m.Show(dataGridView1, new Point(e.X, e.Y));
}
}
While this question is old, the answers aren't proper. Context menus have their own events on DataGridView. There is an event for row context menu and cell context menu.
The reason for which these answers aren't proper is they do not account for different operation schemes. Accessibility options, remote connections, or Metro/Mono/Web/WPF porting might not work and keyboard shortcuts will down right fail (Shift+F10 or Context Menu key).
Cell selection on right mouse click has to be handled manually. Showing the context menu does not need to be handled as this is handled by the UI.
This completely mimics the approach used by Microsoft Excel. If a cell is part of a selected range, the cell selection doesn't change and neither does CurrentCell. If it isn't, the old range is cleared and the cell is selected and becomes CurrentCell.
If you are unclear on this, CurrentCell is where the keyboard has focus when you press the arrow keys. Selected is whether it is part of SelectedCells. The context menu will show on right click as handled by the UI.
private void dgvAccount_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex != -1 && e.RowIndex != -1 && e.Button == System.Windows.Forms.MouseButtons.Right)
{
DataGridViewCell c = (sender as DataGridView)[e.ColumnIndex, e.RowIndex];
if (!c.Selected)
{
c.DataGridView.ClearSelection();
c.DataGridView.CurrentCell = c;
c.Selected = true;
}
}
}
Keyboard shortcuts do not show the context menu by default, so we have to add them in.
private void dgvAccount_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.F10 && e.Shift) || e.KeyCode == Keys.Apps)
{
e.SuppressKeyPress = true;
DataGridViewCell currentCell = (sender as DataGridView).CurrentCell;
if (currentCell != null)
{
ContextMenuStrip cms = currentCell.ContextMenuStrip;
if (cms != null)
{
Rectangle r = currentCell.DataGridView.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, false);
Point p = new Point(r.X + r.Width, r.Y + r.Height);
cms.Show(currentCell.DataGridView, p);
}
}
}
}
I've reworked this code to work statically, so you can copy and paste them into any event.
The key is to use CellContextMenuStripNeeded since this will give you the context menu.
Here's an example using CellContextMenuStripNeeded where you can specify which context menu to show if you want to have different ones per row.
In this context MultiSelect is True and SelectionMode is FullRowSelect. This is just for the example and not a limitation.
private void dgvAccount_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.RowIndex == -1 || e.ColumnIndex == -1)
return;
bool isPayment = true;
bool isCharge = true;
foreach (DataGridViewRow row in dgv.SelectedRows)
{
if ((string)row.Cells["P/C"].Value == "C")
isPayment = false;
else if ((string)row.Cells["P/C"].Value == "P")
isCharge = false;
}
if (isPayment)
e.ContextMenuStrip = cmsAccountPayment;
else if (isCharge)
e.ContextMenuStrip = cmsAccountCharge;
}
private void cmsAccountPayment_Opening(object sender, CancelEventArgs e)
{
int itemCount = dgvAccount.SelectedRows.Count;
string voidPaymentText = "&Void Payment"; // to be localized
if (itemCount > 1)
voidPaymentText = "&Void Payments"; // to be localized
if (tsmiVoidPayment.Text != voidPaymentText) // avoid possible flicker
tsmiVoidPayment.Text = voidPaymentText;
}
private void cmsAccountCharge_Opening(object sender, CancelEventArgs e)
{
int itemCount = dgvAccount.SelectedRows.Count;
string deleteChargeText = "&Delete Charge"; //to be localized
if (itemCount > 1)
deleteChargeText = "&Delete Charge"; //to be localized
if (tsmiDeleteCharge.Text != deleteChargeText) // avoid possible flicker
tsmiDeleteCharge.Text = deleteChargeText;
}
private void tsmiVoidPayment_Click(object sender, EventArgs e)
{
int paymentCount = dgvAccount.SelectedRows.Count;
if (paymentCount == 0)
return;
bool voidPayments = false;
string confirmText = "Are you sure you would like to void this payment?"; // to be localized
if (paymentCount > 1)
confirmText = "Are you sure you would like to void these payments?"; // to be localized
voidPayments = (MessageBox.Show(
confirmText,
"Confirm", // to be localized
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2
) == DialogResult.Yes);
if (voidPayments)
{
// SQLTransaction Start
foreach (DataGridViewRow row in dgvAccount.SelectedRows)
{
//do Work
}
}
}
private void tsmiDeleteCharge_Click(object sender, EventArgs e)
{
int chargeCount = dgvAccount.SelectedRows.Count;
if (chargeCount == 0)
return;
bool deleteCharges = false;
string confirmText = "Are you sure you would like to delete this charge?"; // to be localized
if (chargeCount > 1)
confirmText = "Are you sure you would like to delete these charges?"; // to be localized
deleteCharges = (MessageBox.Show(
confirmText,
"Confirm", // to be localized
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2
) == DialogResult.Yes);
if (deleteCharges)
{
// SQLTransaction Start
foreach (DataGridViewRow row in dgvAccount.SelectedRows)
{
//do Work
}
}
}
Put a context menu on your form, name it, set captions etc. using the built-in editor
Link it to your grid using the grid property ContextMenuStrip
For your grid, create an event to handle CellContextMenuStripNeeded
The Event Args e has useful properties e.ColumnIndex, e.RowIndex.
I believe that e.RowIndex is what you are asking for.
Suggestion: when user causes your event CellContextMenuStripNeeded to fire, use e.RowIndex to get data from your grid, such as the ID. Store the ID as the menu event's tag item.
Now, when user actually clicks your menu item, use the Sender property to fetch the tag. Use the tag, containing your ID, to perform the action you need.
Use the CellMouseDown event on the DataGridView. From the event handler arguments you can determine which cell was clicked. Using the PointToClient() method on the DataGridView you can determine the relative position of the pointer to the DataGridView, so you can pop up the menu in the correct location.
(The DataGridViewCellMouseEvent parameter just gives you the X and Y relative to the cell you clicked, which isn't as easy to use to pop up the context menu.)
This is the code I used to get the mouse position, then adjust for the position of the DataGridView:
var relativeMousePosition = DataGridView1.PointToClient(Cursor.Position);
this.ContextMenuStrip1.Show(DataGridView1, relativeMousePosition);
The entire event handler looks like this:
private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
// Ignore if a column or row header is clicked
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
// Here you can do whatever you want with the cell
this.DataGridView1.CurrentCell = clickedCell; // Select the clicked cell, for instance
// Get mouse position relative to the vehicles grid
var relativeMousePosition = DataGridView1.PointToClient(Cursor.Position);
// Show the context menu
this.ContextMenuStrip1.Show(DataGridView1, relativeMousePosition);
}
}
}
Follow the steps:
Create a context menu like:
User needs to right click on the row to get this menu. We need to handle the _MouseClick event and _CellMouseDown event.
selectedBiodataid is the variable that contains the selected row information.
Here is the code:
private void dgrdResults_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
}
}
private void dgrdResults_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
//handle the row selection on right click
if (e.Button == MouseButtons.Right)
{
try
{
dgrdResults.CurrentCell = dgrdResults.Rows[e.RowIndex].Cells[e.ColumnIndex];
// Can leave these here - doesn't hurt
dgrdResults.Rows[e.RowIndex].Selected = true;
dgrdResults.Focus();
selectedBiodataId = Convert.ToInt32(dgrdResults.Rows[e.RowIndex].Cells[1].Value);
}
catch (Exception)
{
}
}
}
and the output would be:
Simply drag a ContextMenu or ContextMenuStrip component into your form and visually design it, then assign it to the ContextMenu or ContextMenuStrip property of your desired control.
For the position for the context menu, y found the problem that I needed a it to be relative to the DataGridView, and the event I needed to use gives the poistion relative to the cell clicked. I haven't found a better solution so I implemented this function in the commons class, so I call it from wherever I need.
It's quite tested and works well. I Hope you find it useful.
/// <summary>
/// When DataGridView_CellMouseClick ocurs, it gives the position relative to the cell clicked, but for context menus you need the position relative to the DataGridView
/// </summary>
/// <param name="dgv">DataGridView that produces the event</param>
/// <param name="e">Event arguments produced</param>
/// <returns>The Location of the click, relative to the DataGridView</returns>
public static Point PositionRelativeToDataGridViewFromDataGridViewCellMouseEventArgs(DataGridView dgv, DataGridViewCellMouseEventArgs e)
{
int x = e.X;
int y = e.Y;
if (dgv.RowHeadersVisible)
x += dgv.RowHeadersWidth;
if (dgv.ColumnHeadersVisible)
y += dgv.ColumnHeadersHeight;
for (int j = 0; j < e.ColumnIndex; j++)
if (dgv.Columns[j].Visible)
x += dgv.Columns[j].Width;
for (int i = 0; i < e.RowIndex; i++)
if (dgv.Rows[i].Visible)
y += dgv.Rows[i].Height;
return new Point(x, y);
}
There is simple answer to this topic and that is to use CellMouseDown
After designing your ContextMenu
// somewhere in your code
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add(new MenuItem("Option1"));
cm.MenuItems.Add(new MenuItem("Option2"));
Assign it to the DataGridView
myDataView.ContextMenu = cm;
Get Data from the CLICKED cell without changing the Selected one
private void myDataView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
string myData = myDataView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
// Now ADD the captured DATA to the ContextMenu
cm.MenuItems.Add(new MenuItem("myData"));
// OR if you are doing it by the designer and YOU ALREADY have an Item
// you can simply change the name of it in the designer page to your
// desired one and then just change the Text of it
MenuItem_BTN.Text = "$Properties of {myData}";
}
}
i've a DataGridView on which i've added a DataGridViewCheckBoxColumn.
I've also addedd two buttons one for select all the checkBox and one for de-select all the checkBox.
Cell click and select/deselect all buttons works separately, but doesn't works if, for example, i select a cell with mouse click (check box is selected on UI) and then if i press the deselect-All button the value of checkboxs changed but the checkboxs on UI stay checked.
here is my code
Cell Click
private void dictionaryDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == dictionaryDataGrid.Columns["ColSel"].Index) && (e.RowIndex >= 0))
dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value = !(bool)(dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value == null ? false : dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value);
}
button select all:
private void btn_selAll_Click(object sender, EventArgs e)
{
for (int i = 1; i <= dictionaryDataGrid.Rows.Count; i++)
{
dictionaryDataGrid.Rows[i - 1].Cells["ColSel"].Value = true;
}
}
button deselect all:
private void btn_unselAll_Click(object sender, EventArgs e)
{
for (int i = 1; i <= dictionaryDataGrid.Rows.Count; i++)
{
dictionaryDataGrid.Rows[i - 1].Cells["ColSel"].Value = false;
}
}
Try this :
private void btn_unselAll_Click(object sender, EventArgs e)
{
dictionaryDataGrid.ClearSelection();
foreach (DataGridViewRow row in dictionaryDataGrid.Rows)
{
((DataGridViewCheckBoxCell)row.Cells["ColSel"]).Value = false;
}
}
I have a DataGridView on a form. When I right-click a row, I need the program to open a context menu. With this context menu I want to be able to modify the data in the DataGridView.
I have gotten the context menu to show where I right click, but I don't know where to go from here. As I will be deleting (for example) an entire row, I need to get the index of said row and also set it to selected. I tried this with the cell_clicked event but I can't determine if the left or right mouse button was pressed. But with the mouse_click event I cannot get the row index.
Here is my code:
public Form()
{
ContextMenu contextMenu = new ContextMenu();
//Fill Context Menu
MenuItem delete = new MenuItem("Delete");
contextMenu.MenuItems.Add(delete);
}
private void grdSchedules_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));
//Get rowindex here and select row
}
}
I have tried it this way:
private void grdSchedules_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right) //e.Button does not work here
{
contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));
}
}
I created a more simple and faster generic method which works with any datagrids. This method allows selecting rows with a right click. Add this method to your DataGridViews' "MouseDown" event:
public void DataGridView_RightMouseDown_Select(object sender, MouseEventArgs e)
{
// If the user pressed something else than mouse right click, return
if (e.Button != System.Windows.Forms.MouseButtons.Right) { return; }
DataGridView dgv = (DataGridView)sender;
// Use HitTest to resolve the row under the cursor
int rowIndex = dgv.HitTest(e.X, e.Y).RowIndex;
// If there was no DataGridViewRow under the cursor, return
if (rowIndex == -1) { return; }
// Clear all other selections before making a new selection
dgv.ClearSelection();
// Select the found DataGridViewRow
dgv.Rows[rowIndex].Selected = true;
}
I have found a solution. Here is how I did it:
private void grdSchedules_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
int currentMouseOverRow = grdSchedules.HitTest(e.X, e.Y).RowIndex;
for (int x = 0; x < grdSchedules.Rows.Count; x++)
{
if (grdSchedules.Rows[x].Index == currentMouseOverRow)
{
grdSchedules.Rows[x].Selected = true;
}
else
{
grdSchedules.Rows[x].Selected = false;
}
}
contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));
}
}
You could've used grdSchedules_MouseDown or grdSchedules_MouseUp events instead of grdSchedules_MouseClick or grdSchedules_CellClick.
I have a datagridview control on winforms with the following event to drag 'row' out of datagrid:
private void gridOperations_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (Math.Abs(e.X - mouseDownPos.X) >= SystemInformation.DoubleClickSize.Width || Math.Abs(e.Y - mouseDownPos.Y) >= SystemInformation.DoubleClickSize.Height)
{
string[] filesToDrag = { "tmp/generated.log" };
gridOperations.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
}
}
}
The problem is I also have click and double click events on datagridview, the double click event almost never gets executed at first, unless I don't move my mouse at all when clicking. how can I add a 'thershold' so that if I hold mouse down and drag cell for 3 pixels, then it will trigger gridOperations.DoDragDrop? Thanks!
try to use an integer counter.
Every time the Event is fired you increment the int and if it is reaches your treshold you execute the rest of the Code and reset it to 0.
like:
private int thCount = 0;
private void gridOperations_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left && thCount==5)
{
//...
thCount = 0;
}
else
{
thCount++;
}
}
hope i could help you
I want to override the behavior of a mouse click in the DataGridView header/column cell (top, left cell). That cell causes all rows to be selected. Instead, I want to stop it from selecting all rows. I see an event for RowHeaderSelect and ColumnHeaderSelect but not one for that top, left header cell.
Any ideas? Am I just being blind?
This is the dissasembled code of what happens when you click that cell:
private void OnTopLeftHeaderMouseDown()
{
if (this.MultiSelect)
{
this.SelectAll();
if (-1 != this.ptCurrentCell.X)
{
this.SetCurrentCellAddressCore(this.ptCurrentCell.X, this.ptCurrentCell.Y, false, false, false);
}
}
In order for you to prevent this behavior you have 2 solutions:
Disable multi selection (if your business logic permits)
Inherit your own datagrid and override OnCellMouseDown (something like this)
protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == -1) return;
base.OnCellMouseDown(e);
}
I know this is late but hopefully it will help someone. The code below worked for me in a similar scenario.
private void MyDataGridView_MouseUp(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hitInfo = this.MyDataGridView.HitTest(e.X, e.Y);
if (hitInfo.Type == DataGridViewHitTestType.TopLeftHeader)
{
MyDataGridView.ClearSelection();
}
}
You could gain some control over the click event using this hack :)
private void dataGridView1_Click(object sender, EventArgs e)
{
MouseEventArgs args = (MouseEventArgs)e;
DataGridView dgv = (DataGridView)sender;
DataGridView.HitTestInfo hit = dgv.HitTest(args.X, args.Y);
if (hit.Type == DataGridViewHitTestType.TopLeftHeader)
{
// do something here
}
}
override OnCellMouseDown method:
if (e.ColumnIndex == -1 && e.RowIndex == -1)
{
return;
}
else
{
base.OnCellMouseDown(e);
}