您的位置:首页 > 其它

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

2017-05-07 09:44 302 查看
通过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         }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: