網站首頁 編程語言 正文
自定義單元格表示值
通過CellFormatting事件,可以自定義單元格的表示值。(比如:值為Error的時候,單元格被設定為紅色)
示例:
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.Columns[e.ColumnIndex].Name=="Sex") { string value = e.Value.ToString(); if (value.Equals("女")) { e.Value = "Woman"; e.FormattingApplied = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } }
Error圖標顯示
為了提醒用戶注意,DataGridView可以使用Error圖標來突出顯示。
Error圖標可以在單元格和行頭內表示,但不能在列頭上顯示。
1、ErrorText屬性
當設定單元格/行的ErrorText屬性的內容后,單元格/行的Error圖標就會被表示出來。另外,只有在DataGridView.ShowCellErrors=True時,Error圖標才能顯示。(默認屬性為True)
設定(0,0)的單元格表示Error圖標
this.dgv_Users[0, 0].ErrorText = "只能輸入男或女";
設定第4行的行頭顯示Error圖標
this.dgv_Users.Rows[3].ErrorText = "不能輸入負數";
2、CellErrorTextNeeded、RowErrorTextNeeded事件
即時輸入時的Error圖標的表示,可以使用CellErrorTextNeeded事件。同時,在大量的數據處理時,需要進行多處的內容檢查并顯示Error圖標的應用中,遍歷單元格設定ErrorText的方法是效率低下的,應該使用CellErrorTextNeeded事件。行的Error圖標的設定則應該使用RowErrorTextNeeded事件。但是,需要注意的是當DataSource屬性設定了VirtualMode=True時,上述事件則不會被觸發。
CellErrorTextNeeded、RowErrorTextNeeded事件一般在需要保存數據時使用,保存數據之前先判斷單元格輸入的值是否合法,如果不合法,則在不合法的單元格或行顯示Error圖標。相當于做了一個客戶端的驗證。
private void dgv_Users_CellErrorTextNeeded(object sender, DataGridViewCellErrorTextNeededEventArgs e) { DataGridView dgv=sender as DataGridView; if (dgv.Columns[e.ColumnIndex].Name.Equals("Sex")) { string value = dgv[e.ColumnIndex, e.RowIndex].Value.ToString(); if (!value.Equals("男") && !value.Equals("女")) { e.ErrorText = "只能輸入男或女"; } } }
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和Password列必須輸入值"; } }
原文鏈接:https://www.cnblogs.com/dotnet261010/p/6819062.html
相關推薦
- 2022-08-23 C++深入探究友元使用_C 語言
- 2022-06-02 C語言結構體(struct)的詳細講解_C 語言
- 2022-06-20 elasticsearch索引index之merge底層機制的合并講解_相關技巧
- 2022-06-14 詳解Python中生成隨機數據的示例詳解_python
- 2022-07-08 go語言代碼生成器code?generator使用示例介紹_Golang
- 2022-02-27 Spring Boot -- 創建工程時 Spring Initializr 報錯 Error:co
- 2023-01-14 python的數據與matlab互通問題:SciPy_python
- 2022-04-03 C++內嵌匯編示例詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支