DataGridView使用技巧十:单元格表示值的自定义
通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色)
示例:
1 private void dgv_Users_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 2 { 3 try 4 { 5 if (e == null || e.Value == null || !(sender is DataGridView)) 6 return; 7 DataGridView dgv = sender as DataGridView; 8 if (dgv.Columns[e.ColumnIndex].Name=="Sex") 9 { 10 string value = e.Value.ToString(); 11 if (value.Equals("女")) 12 { 13 e.Value = "Woman"; 14 e.FormattingApplied = true; 15 } 16 } 17 } 18 catch (Exception ex) 19 { 20 MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); 21 } 22 }
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决