c# - Right click to select a row in a Datagridview and show a menu to delete it -


i have few columns in datagridview, , there data in rows. saw few solutions in here, can not combine them!

simply way right-click on row, select whole row , show menu option delete row , when option selected delete row.

i made few attempts none working , looks messy. should do?

i solved it:

  • in visual studio, create contextmenustrip item called "deleterow"

  • then @ datagridview link contextmenustrip

using code below helped me getting work.

this.mydatagridview.mousedown += new system.windows.forms.mouseeventhandler(this.mydatagridview_mousedown); this.deleterow.click += new system.eventhandler(this.deleterow_click); 

here cool part

private void mydatagridview_mousedown(object sender, mouseeventargs e) {     if(e.button == mousebuttons.right)     {         var hti = mydatagridview.hittest(e.x, e.y);         mydatagridview.clearselection();         mydatagridview.rows[hti.rowindex].selected = true;     } }  private void deleterow_click(object sender, eventargs e) {     int32 rowtodelete = mydatagridview.rows.getfirstrow(datagridviewelementstates.selected);     mydatagridview.rows.removeat(rowtodelete);     mydatagridview.clearselection(); } 

i hope code others :-)

i welcome correction if there error.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -