码迷,mamicode.com
首页 > Windows程序 > 详细

DataGridView使用技巧十:单元格表示值的自定义

时间:2017-05-07 11:33:29      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:ring   private   定义   index   ace   tac   ack   value   trace   

通过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         }

 

DataGridView使用技巧十:单元格表示值的自定义

标签:ring   private   定义   index   ace   tac   ack   value   trace   

原文地址:http://www.cnblogs.com/dotnet261010/p/6819062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!