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

How to customize the cell representation value and Error icon display by DataGridView

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

Share

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

Editor to share with you DataGridView how to customize the cell representation value, Error icon display, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Custom cell representation value

Through the CellFormatting event, you can customize the representation value of the cell. (for example, when the value is Error, the cell is set to red)

Example:

Private void dgv_Users_CellFormatting (object sender, DataGridViewCellFormattingEventArgs e) {try {if (e = = null | | e.Value = = null | |! (sender is DataGridView)) return; DataGridView dgv = sender as DataGridView If (dgv.ColumnIndex]. Name = = "Sex") {string value = e.Value.ToString (); if (value.Equals ("female")) {e.Value = "Woman"; e.FormattingApplied = true } catch (Exception ex) {MessageBox.Show (ex.Message + "\ r\ n" + ex.StackTrace);}} Error icon display

To remind users, DataGridView can use the Error icon to highlight it.

The Error icon can be represented in cells and headers, but cannot be displayed on column headers.

1. ErrorText attribute

When the contents of the ErrorText property of the cell / row are set, the Error icon of the cell / row is represented. In addition, the Error icon can be displayed only when DataGridView.ShowCellErrors=True. (default property is True)

The cell of the setting (0mem0) represents the Error icon.

This.dgv_Users [0,0] .ErrorText = "only men or women can be entered"

Set the line header of line 4 to display the Error icon

This.dgv_Users.Rows [3] .ErrorText = "cannot enter negative numbers"; 2. CellErrorTextNeeded, RowErrorTextNeeded events

For the representation of the Error icon for instant input, you can use the CellErrorTextNeeded event. At the same time, in the application of a large amount of data processing, which requires multiple content checks and displays the Error icon, the method of traversing the cell to set ErrorText is inefficient, and the CellErrorTextNeeded event should be used. The Error icon for the row should be set using the RowErrorTextNeeded event. However, it is important to note that the above events are not triggered when the DataSource property is set to VirtualMode=True.

CellErrorTextNeeded and RowErrorTextNeeded events are generally used when you need to save data. Before saving the data, determine whether the value entered in the cell is legal, and if not, display the Error icon in illegal cells or rows. It is equivalent to doing a client-side verification.

Private void dgv_Users_CellErrorTextNeeded (object sender, DataGridViewCellErrorTextNeededEventArgs e) {DataGridView dgv=sender as DataGridView; if (dgv.ColumnIndex] .Name.Equals ("Sex")) {string value = dgv [e.ColumnIndex, e.RowIndex] .Value.ToString () If (! value.Equals) & &! value.Equals (female) {e.ErrorText = "only male or female";}} private void dgv_Users_RowErrorTextNeeded (object sender, DataGridViewRowErrorTextNeededEventArgs e) {DataGridView dgv = sender as DataGridView If (dgv ["UserName", e.RowIndex]. Value = = DBNull.Value & & dgv ["Password", e.RowIndex]. Value = = DBNull.Value) {e.ErrorText = "UserName and Password columns must enter values";}} above is all the content of the article "how DataGridView customizes cell representation values and Error icons are displayed". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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