1. Right-click to delete data when you move the cursor over the data row.
Step 1. first define the variable currentrowindex to store the number of rows passing by the mouse.
As follows:
View code 1 Public Partial Class Formtest: Form
2 {
3 Private Bool Isload { Get ; Set ;}
4 /// <Summary>
5 /// Current row number
6 /// </Summary>
7 Private Int Currentrowindex { Get ; Set ;}
2. assign a value to the variable. Use cellmouseenter
CodeAs follows:
View code 1 Private Void Maid ( Object Sender, datagridviewcelleventargs E)
2 {
3 VaR dgv = (Datagridview) sender;
4 Currentrowindex = E. rowindex;
5 Currentcolumnindex = E. columnindex;
6
7 }
3. Right-click and click Delete,
The Code is as follows:
View code 1 Private Void Removetoolstripmenuitem_click ( Object Sender, eventargs E)
2 {
3 // Maid rowcollection = new maid (maid );
4 Datagridviewrow row = Datagridview1.rows [currentrowindex];
5 Datagridview1.rows. Remove (ROW );
6 // Prevent the scroll bar from rolling to a place that is not desired.
7 Datagridview1.currentcell = Datagridview1 [ 0 , Currentrowindex];
8
9 }
4. Row deletion is a condition judgment process.
4.1 when you press the Del key, execute the following function:
View code 1 Private Void Datagridview1_userdeletingrow ( Object Sender, datagridviewrowcanceleventargs E)
2 {
3 // Confirm the user before deletion.
4 If (MessageBox. Show ( " Are you sure you want to delete this row of data? " , " Confirm deletion " ,
5 Messageboxbuttons. okcancel,
6 Messageboxicon. Question,
7 Messageboxdefaultbutton. button2) ! = Dialogresult. OK)
8 {
9 // If not OK, cancel.
10 E. Cancel = True ;
11 }
12 }
4.2 If you right-click to delete a file, you can execute the following function.
View code 1 Private Void Maid ( Object Sender, datagridviewrowsremovedeventargs E)
2 {
3 If ( ! Isload)
4 {
5 If (MessageBox. Show (
6 " Are you sure you want to delete the data? " ,
7 " Confirm deletion " ,
8 Messageboxbuttons. okcancel,
9 Messageboxicon. Question,
10 Messageboxdefaultbutton. button2) = Dialogresult. OK
11 )
12 {
13 Int Removeindex = E. rowindex;
14 // String removecolumnonetext = convert. tostring (maid [0, E. rowindex]. value );
15 MessageBox. Show ( " Your data has been deleted: " + Removeindex );
16 }
17
18 }
19
20 }