C# - DatagridView and ContextMenu - c#

I have a datagridview where I show infomation about products. I want to bind a contextmenu when the user selects a cell and then right clicks on that cell. I have another contextmenu and that one is bound to the columns of the datagridview. If a user right clicks on a column the contextmenu shows.
I have tried like this but it does not work. The context menu shows when the user right clicks on a cell, but the contextmenu that is bound to the column header does not work.
private void GridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
productContextMenu.Show(GridView1, e.Location);
}
}
How do I make it so that when the user right clicks on a datagridview shows up?
Many thanx in advance.
EDIT
Thnx for the answers. I solved the problem like this:
private void GridView1_MouseUp(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hitTestInfo;
if (e.Button == MouseButtons.Right)
{
hitTestInfo = GridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.Cell)
{
productContextMenu.Show(GridView1, e.Location);
}
}
}
Both the contextmenus shows. When I click on the column that context menu shows, and when I click on a cell that contextmenu shows.

Try this
private void dataGridView1_CellMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenu.Show(datagridview, e.Location);
}
}
or
private void dataGridView_MouseUp(object sender, MouseEventArgs e)
{
// Load context menu on right mouse click
DataGridView.HitTestInfo hitTestInfo;
if (e.Button == MouseButtons.Right)
{
hitTestInfo = dataGridView.HitTest(e.X, e.Y);
// If column is first column
if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
contextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y));
// If column is second column
if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
contextMenuForColumn2.Show(dataGridView, new Point(e.X, e.Y));
}
}

For the problem with the relative position, you can also use this aproach:
DataGridViewColumn dgvC = new DataGridViewColumn();
DataGridViewRow dgvR = new DataGridViewRow();
dgvC = dgv.Columns[e.ColumnIndex];
dgvR = dgv.Rows[e.RowIndex];
Point p = new Point();
p.X = (dgvC.Width * e.ColumnIndex) + e.X;
p.Y = (dgvR.Height * e.RowIndex) + e.Y;
dgv.ContextMenu.Show(dgv, p);

Related

ContextManuStripTool.show() position is wrong

I'm trying to learn how to use the ContextMenuStrip
when using this code :
private void DG_dataGridView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hitTest = DG_dataGridView.HitTest(e.X, e.Y);
if (hitTest.Type == DataGridViewHitTestType.ColumnHeader)//currentMouseOverRow >= 0)
{
string colName = DG_dataGridView.Columns[hitTest.ColumnIndex].Name;
GlobalParam.Insatance.ClickData = new RightClickData(hitTest, colName);
RightClickToolStrip.Show(DG_dataGridView, new Point(e.X, e.Y));
}
}
}
I see the menu pop up in the right position
but when instead I use this code :
private void DG_dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hitTest = DG_dataGridView.HitTest(e.Location.X, e.Location.Y);
string colName = DG_dataGridView.Columns[e.ColumnIndex].Name;
GlobalParam.Insatance.CustomMouseGridClickData = new CustomMouseOnGridClickData(e, hitTest.Type, colName);
RightClickToolStrip.Show(DG_dataGridView, new Point(e.X, e.Y));
}
}
I see the menu pop on the top left corner of my DataGridView
I know this is due to the operation of DataGridViewCellMouseEventArgs
I have tried to set other control in the toolStrip.Show method without success
what is the proper way to get my click position
With the ColumnHeaderMouseClick event, you don't have to test for the column getting clicked or not using that HitTest method. Also, I think the coordinates are relative to the column being clicked, so you can try using the GetColumnDisplayRectangle function to offset it property:
void DG_dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
if (e.ColumnIndex > -1) {
Rectangle r = DG_dataGridView.GetColumnDisplayRectangle(e.ColumnIndex, true);
RightClickToolStrip.Show(DG_dataGridView, r.Left + e.X, r.Top + e.Y);
}
}
}

Select only one cell in datagridview when rightclick

in my application i have a datagridview with many rows. i want to add a feature to pop up context menu when user right click in certain cell. but only way to select a cell is left click in default. so i searched little bit and i found this code
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = dataGridView1.HitTest(e.X, e.Y);
dataGridView1.ClearSelection();
dataGridView1.Rows[hti.RowIndex].Selected = true;
}
}
but the problem of this code is after i put this code to mousedown when i right click wholw row is selected.but i only want to select one cell
how can i fix this
To get the behaviour you want you have to:
-Make sure you have in your dataGridView1 properties SelectionMode = CellSelect or SelectionMode = RowHeaderSelect (depending if you want the user to be able to select the whole row)
-Make these changes on your code:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = dataGridView1.HitTest(e.X, e.Y);
if (hti.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = dataGridView1[hti.ColumnIndex, hti.RowIndex];
}
}
}

Select row in DataGridView in mouse_click event

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.

Right click to select row in DataGridView

I need to select a row in a DataGridView with right click before a ContextMenu is shown because the ContextMenu is row-dependent.
I've tried this:
if (e.Button == MouseButtons.Right)
{
var hti = dataGrid.HitTest(e.X, e.Y);
dataGrid.ClearSelection();
dataGrid.Rows[hti.RowIndex].Selected = true;
}
or:
private void dataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGrid.Rows[e.RowIndex].Selected = true;
dataGrid.Focus();
}
}
This works but when I try to read dataGrid.Rows[CurrentRow.Index] I see only the row selected with left click and not those selected with right click.
Try setting the current cell like this (this will set the CurrentRow property of the DataGridView before the context menu item is selected):
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
var dataGrid = (DataGridView) sender;
if (e.Button == MouseButtons.Right && e.RowIndex != -1)
{
var row = dataGrid.Rows[e.RowIndex];
dataGrid.CurrentCell = row.Cells[e.ColumnIndex == -1 ? 1 : e.ColumnIndex];
row.Selected = true;
dataGrid.Focus();
}
}
I realize this thread is old, I just wanted to add one thing: If you want to be able to select, and perform the action, on multiple rows: you can check to see if the row you are right-clicking is already selected. This way the DataGridview behaves likes a ListView in this regard. So right clicking on a row not already selected: selects this row and open the context menu. Right clicking on a row already selected just gives you the context menu and keep the selected rows as expected.
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewRow clickedRow = (sender as DataGridView).Rows[e.RowIndex];
if (!clickedRow.Selected)
dataGridView1.CurrentCell = clickedRow.Cells[e.ColumnIndex];
var mousePosition = dataGridView1.PointToClient(Cursor.Position);
ContextMenu1.Show(dataGridView1, mousePosition);
}
}
}
private void grid_listele_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
grid_listele.ClearSelection();
grid_listele[e.ColumnIndex, e.RowIndex].Selected = true;
}
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
var hti = GridView.HitTest(e.X, e.Y);
GridView.ClearSelection();
int index = hti.RowIndex;
if (index >= 0)
{
GridView.Rows[hti.RowIndex].Selected = true;
LockFolder_ContextMenuStrip.Show(Cursor.Position);
}
}
This is Accurate Method i Guess

How to select dgvRow by rightClick?

I have a contextMenu on a DataGridView.
It shows options for manipulating with selectedRow.
User must have leftClick - to selecet a row - and then rightClick - to open contextMenu.
I want to omit leftClic, i.e. - select a dgvRow by rightClick and at the same time - open contextMenu.
Is it possible ?
here you go try this.
add to your datagridview_MouseDown event
private void dgvPermit_MouseDown(object sender, MouseEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
DataGridView.HitTestInfo Hti;
if (e.Button == MouseButtons.Right)
{
Hti = dgv.HitTest(e.X, e.Y);
if (Hti.Type == DataGridViewHitTestType.Cell)
{
if (!((DataGridViewRow)(dgv.Rows[Hti.RowIndex])).Selected)
{
dgv.ClearSelection();
((DataGridViewRow)dgv.Rows[Hti.RowIndex]).Selected = true;
}
}
}
}

Categories

Resources