DataGridView单元格内容换行显示及设置背景色

  类准备不复述了,点这里

  说下思路:DataGridView单元格显示内容是通过对象的ToString()方法获取的,所以重写要换行的列的ToString()方法即可。实现换行需要设置DataGridView的两个属性:1、在属性窗口找到AutoSizeRowsMode,设置为AllCells(此设置表示让行间距适应单元格内容高度);2、在属性窗口找到DefaultCellStyle,打开属性编辑窗口,设置WrapMode为true(此设置表示单元格内容长度超出则换行显示)。下面给出增加的代码。

  新增类Col_3,如下:

 1         class Col_3
 2         {
 3             int _dto, _dfo;
 4 
 5             public Col_3(int dto, int dfo)
 6             {
 7                 _dto = dto;
 8                 _dfo = dfo;
 9             }
10 
11             public override string ToString()
12             {
13                 return string.Format("DTO:{0}{1}DFO:{2}", _dto, Environment.NewLine, _dfo);
14             }
15         }
View Code

  在Character类中增加Col_3属性,如下:

 1         class Character : INotifyPropertyChanged
 2         {
 3             string _name;
 4             [DisplayName("你的名字")]  // 这是显示的列名称哦
 5             public string Name
 6             {
 7                 get { return _name; }
 8                 set
 9                 {
10                     if (_name != value)
11                     {
12                         _name = value;
13                         NotifyPropertyChanged("Name");
14                     }
15                 }
16             }
17 
18             // 2) 在属性改变时引发事件
19             int _age;
20             [DisplayName("他的年纪")]
21             public int Age
22             {
23                 get { return _age; }
24                 set
25                 {
26                     if (_age != value)
27                     {
28                         _age = value;
29                         NotifyPropertyChanged("Age");
30                     }
31                 }
32             }
33 
34             Col_3 _customCol;
35             public Col_3 CustomCol
36             {
37                 get { return _customCol; }
38                 set
39                 {
40                     if (_customCol != value)
41                     {
42                         _customCol = value;
43                         NotifyPropertyChanged("CustomCol");
44                     }
45                 }
46             }
47 
48             PropertyChangedEventHandler _propertyChanged;
49             public event PropertyChangedEventHandler PropertyChanged
50             {
51                 add { _propertyChanged += value; }
52                 remove { _propertyChanged -= value; }
53             }
54 
55             private void NotifyPropertyChanged(string property_name)
56             {
57                 _propertyChanged?.Invoke(this, new PropertyChangedEventArgs(property_name));
58             }
59         }  /* end of class Character */
View Code

  添加CellFormatting事件处理,根据内容设置背景,如下:

 1         private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
 2         {
 3             if (e.RowIndex < 0 || e.ColumnIndex < 0)
 4                 return;
 5 
 6             if (e.Value is Col_3)   // if( (e.Value as Col_3)?.DTO > 0 ) ...
 7                 e.CellStyle.BackColor = Color.Green;
 8             else if (e.Value is string)
 9                 e.CellStyle.BackColor = Color.Salmon;
10             else if (e.Value is int)
11                 e.CellStyle.BackColor = Color.Blue;
12         }
View Code

  在Init()方法中添加数据测试,如下:

 1         void Init()
 2         {
 3             _dtSource = new DataTable();
 4             _bdList = new BindingList<Character>();
 5             _bdSource = new BindingSource();
 6             _bdSource.DataSource = _bdList;  // 3) 绑定数据源,由BindingSoource组件处理细节
 7 
 8             _dtSource.Columns.Add(new DataColumn("Name"));
 9             _dtSource.Columns.Add(new DataColumn("Age"));
10 
11             DataRow dr;
12 
13             dr = _dtSource.NewRow();
14             dr["Name"] = "1";
15             dr["Age"] = "21";
16             _dtSource.Rows.Add(dr);
17 
18             dr = _dtSource.NewRow();
19             dr[0] = "2";
20             dr[1] = "23";
21             _dtSource.Rows.Add(dr);
22 
23             _dtSource.Rows.Add(3, 27);
24 
25             _bdList.Add(new Character() { Name = "1", Age = 21, CustomCol = new Col_3(0, 0) });
26             _bdList.Add(new Character() { Name = "2", Age = 23, CustomCol = new Col_3(1, 1) });
27             _bdList.Add(new Character() { Name = "3", Age = 27, CustomCol = new Col_3(2, 2) });
28         }
View Code

效果:

posted @ 2020-11-21 14:02  不会吉他的程序员  阅读(885)  评论(0编辑  收藏  举报