Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of clearing the displayed data and setting the right-click menu by DataGridView

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "DataGridView clears the displayed data and sets the right-click menu". In the daily operation, I believe that many people have doubts about the method of clearing the displayed data and setting the right-click menu by DataGridView. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "DataGridView clears the displayed data and sets the right-click menu". Next, please follow the editor to study!

1. Clear data 1. Clear data when DataGridView unbound data this.dgv_PropDemo.DataSource = null2, clear data when DataGridView binds data

If DataGridView binds data, you cannot use this.dgv_PropDemo.DataSource = null to empty the data. Using this.dgv_PropDemo.DataSource = null will clear not only the data, but also the columns of DataGridView. In this case, use the following code to clear the displayed data:

DataTable dt = this.dgv_PropDemo.DataSource as DataTable;dt.Rows.Clear (); this.dgv_PropDemo.DataSource = dt; II. Set right-click menu

DataGridView,DataGridViewColumn,DataGridViewRow,DataGridViewCell has a ContextMenuStrip attribute. You can control the display of DataGridView's right-click menu by setting the ContextMenuStrip object.

The ContextMenuStrip property of DataGridViewColumn sets the right-click menu for cells other than column headers.

The ContextMenuStrip property of DataGridViewRow sets the right-click menu for cells other than the header.

The ContextMenuStrip property of DataGridViewCell sets the right-click menu for the specified cell.

For the right-click menu settings on the cell, the priority is: Cell > Row > Column > DataGridView

CellContextMenuStripNeeded and RowContextMenuStripNeeded events can be used to set the right-click menu of a cell, especially if the right-click menu needs to change according to the cell value. It is more efficient to use this event to set right-click menus than to use loop traversal.

Description: in the parameters of the CellContextMenuStripNeeded event handling method, e.RowIndex=-1 represents the column header and e.ColumnIndex=-1 represents the line header. E.ColumnIndex=-1 does not exist in RowContextMenuStripNeeded.

Example 1:

/ / right-click menu of DataGridView this.dgv_Users.ContextMenuStrip = cmsDgv;// setting column right-click menu this.dgv_Users.Columns [1] .ContextMenuStrip = cmsColumn;// setting column header right-click menu this.dgv_Users.Columns [1] .HeaderCell.ContextMenuStrip = cmsHeaderCell;// setting row right-click menu this.dgv_Users.Rows [2] .ContextMenuStrip = cmsRow;// setting cell right-click menu this.dgv_Users [1,2] .ContextMenuStrip = cmsCell

Example 2:

Private void dgv_Users_CellContextMenuStripNeeded (object sender, DataGridViewCellContextMenuStripNeededEventArgs e) {DataGridView dgv = sender as DataGridView; if (e.RowIndex < 0) {/ / set column header right e.ContextMenuStrip = cmsHeaderCell;} else if (e.ColumnIndex < 0) {/ / set row right button menu e.ContextMenuStrip = cmsRow } else if (dgv [e.ColumnIndex, e.RowIndex] .Value.ToString (). Equals ("male") {e.ContextMenuStrip = cmsCell;} else {e.ContextMenuStrip = cmsDgv;}} at this point, the study of "DataGridView clears the displayed data and sets the right-click menu" is over. I hope it can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report