Winform datagridview right-click the menu to delete data rows

Source: Internet
Author: User

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 }

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.