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

C # how to operate DataGridView to set cell read-only

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

Share

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

This article mainly introduces how to operate DataGridView to set cell read-only, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

1. Modify the ReadOnly attribute

1. Set the entire DataGridView to be read-only:

DataGridView.ReadOnly=true

At this time, the user's new row addition and deletion operations are also blocked.

2. Set a cell in the DataGridView to be uneditable, for example: set the cell in the second row and the first column to be uneditable:

This.dgv_PropDemo [0,1] .ReadOnly = true

3. Set a column of DataGridVIew that is not editable. For example, set the second column to be uneditable:

This.dgv_PropDemo.Columns [1] .ReadOnly = true

4. Set the entire line of DataGridView to be uneditable, for example: set the third line to be uneditable:

This.dgv_PropDemo.Rows [2] .ReadOnly = true; II, use the EditMode attribute

EditMode property: gets or sets a value indicating how to start editing the cell, which is one of the DataGridViewEditMode enumerated values.

The member name indicates that EditOnEnter can start editing when the cell receives focus. This mode is useful when pressing Tab to enter values horizontally in a row, or Enter to enter values vertically in a column. EditOnF2 press F2 to start editing when the cell gets focus. This mode places the selection point at the end of the cell contents. EditOnKeystroke press any alphanumeric key to start editing when the cell gets focus. EditOnKeystrokeOrF2 press any alphanumeric key or F2 to start editing when the cell gets focus. The default value, EditProgrammatically, starts editing only when the BeginEdit method is called.

Note: with the exception of EditProgrammatically, all DataGridViewEditMode values allow users to start editing a cell by double-clicking it.

When the EditMode property of DataGridView is set to EditProgrammatically, the user cannot manually edit the contents of the cell. However, through the program, you can call the DataGridView.BeginEdit method to put the cell into edit mode for editing.

For example:

This.dgv_PropDemo.BeginEdit (true); 3. Set the non-editable state of the cell according to the condition

When an one-by-one method of setting cell ReadOnly properties through cell coordinates is too troublesome, you can cancel cell editing through the CellBeginEdit event.

Example:

Private void dgv_PropDemo_CellBeginEdit (object sender, DataGridViewCellCancelEventArgs e) {DataGridView dgv = sender as DataGridView / / Unedit if (dgv.Columns [e.ColumnIndex] .Name = = "Sex" & & dgv [e.ColumnIndex, e.RowIndex] .Value.ToString (). Trim () = "male") {/ / unedit e.Cancel = male) when the column name of the current cell is equal to male and the value of the current cell is equal to "male". }} the above is all the contents of the article "how to operate DataGridView to set cells to read-only". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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