Wednesday 15 May 2013

C# DataGridView Right Click to ContextMenu Click Retrieve Cell Value -


I have DataGridView I have created a context menus strip when right-clicking the cell in column 4 of my datagrid view. Although I'm interrupted because I'm clicking ContextMenuStrip menu item, I want to remove data from the cell that was right clicked.

The room I want is the top left corner of the context menustrip, which is right where I right click and tell that place which I want to capture.

I have it so far:

  GridView1.MouseDown + = New MouseEventHandler (this.dataGridView_MouseDown); Private void dataGridView_MouseDown (object sender, MouseEventArgs e) {if (e.Button == MouseButtons.Right) {var ht = dataGridView1.HitTest (e.X, e.Y); // Check for correct column index whether (ht.ColumnIndex == 4 & amp; & amp ;! Ht.RowIndex = -1) {// PO create ContextStripMenu create sub form ContextMenuStrip menu = new ContextMenuStrip (for) ; ToolStrip menu menu menu open.po = new toolstepmenitan ("OpenPO"); MenuOpenPO.MouseDown + = New MouseEventHandler (MenuOpenPO_Click); Menu.Items.AddRange (new toolstepTime [] {MenuOpenPO}); // Data Grid View Datagrams View1.ContextMenuStrip = Assign context menu bar created in the menu; } Other DataGrid View 1. Contains menustrip = null; }}   

But if I change: for Private Zero DataGridView_MouseDown (Object Sender, MouseEventArgs e)

private void dataGridView_MouseDown (object sender, DataGridViewCellMouseEventArgs e)

I'm not sure how to change the GridView1.MouseDown + = new MouseEventHandler (this. DataGridView_MouseDown); So I do not get any error messages or is there a better way to do this?

Final Solution

dataGridView1.CellMouseDown + = this.dataGridView1_CellMouseDown;

  private void dataGridView1_CellMouseDown (object sender, DataGridViewCellMouseEventArgs e) {// Check for correct column index whether (e.Button == MouseButtons.Right & amp; & amp ; e.ColumnIndex == 4 & amp; & amp ;! e.RowIndex = -1) {// PO create ContextStripMenu create sub form ContextMenuStrip menu = new ContextMenuStrip (for); ToolStrip menu menu menu open.po = new toolstepmenitan ("OpenPO"); MenuOpenPO.MouseDown + = New MouseEventHandler (MenuOpenPO_Click); Menu.Items.AddRange (new toolstepTime [] {MenuOpenPO}); // Data Grid View Datagrams View1.ContextMenuStrip = Assign context menu bar created in the menu; CellValu = DataGrid View1.Rows [e.RowIndex] .Cell [E. Column index]. Value Toasting (); } Other DataGrid View 1. Contains menustrip = null; }    

If you are going with the solution of that post, note that Subscribe to the Cellmassdown event, not the mousedown event has a different signature.

In addition, as the Net 2.0, you do not need syntax for all the delegates, you only + = the function that matches the signature of the event representative, such as : // DataGridViewCellMouseEventArgs GridView1.CellMouseDown + = this.dataGridView_MouseDown; with your mouseDown handler function with datagrid view;

Then you will not have an error message, and you can do what you see in the post.

No comments:

Post a Comment