如何:自定义 Windows 窗体 DataGridView 控件中的数据格式设置
下面的代码示例演示如何实现一个 System.Windows.Forms.DataGridView.CellFormatting 事件的处理程序,该处理程序根据单元格的列和值更改单元格的显示方式。
Balance 列中包含负数的单元格被指定为红色背景。也可以将这些单元格的格式设置为货币格式以在负值两边显示圆括号。有关更多信息,请参见如何:设置 Windows 窗体 DataGridView 控件中的数据格式。
Priority 列的单元格显示代替对应的文本单元格值的图像。DataGridViewCellFormattingEventArgs 的 Value 属性既用于获取文本单元格值,也用于设置对应的图像显示值。
1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4 5 public class Form1 : Form 6 { 7 private DataGridView dataGridView1 = new DataGridView(); 8 private Bitmap highPriImage; 9 private Bitmap mediumPriImage; 10 private Bitmap lowPriImage; 11 12 public Form1() 13 { 14 // Initialize the images. 15 try 16 { 17 highPriImage = new Bitmap("highPri.bmp"); 18 mediumPriImage = new Bitmap("mediumPri.bmp"); 19 lowPriImage = new Bitmap("lowPri.bmp"); 20 } 21 catch (ArgumentException) 22 { 23 MessageBox.Show("The Priority column requires Bitmap images " + 24 "named highPri.bmp, mediumPri.bmp, and lowPri.bmp " + 25 "residing in the same directory as the executable file."); 26 } 27 28 // Initialize the DataGridView. 29 dataGridView1.Dock = DockStyle.Fill; 30 dataGridView1.AllowUserToAddRows = false; 31 dataGridView1.Columns.AddRange( 32 new DataGridViewTextBoxColumn(), 33 new DataGridViewImageColumn()); 34 dataGridView1.Columns[0].Name = "Balance"; 35 dataGridView1.Columns[1].Name = "Priority"; 36 dataGridView1.Rows.Add("-100", "high"); 37 dataGridView1.Rows.Add("0", "medium"); 38 dataGridView1.Rows.Add("100", "low"); 39 dataGridView1.CellFormatting += 40 new System.Windows.Forms.DataGridViewCellFormattingEventHandler( 41 this.dataGridView1_CellFormatting); 42 this.Controls.Add(dataGridView1); 43 } 44 45 // Changes how cells are displayed depending on their columns and values. 46 private void dataGridView1_CellFormatting(object sender, 47 System.Windows.Forms.DataGridViewCellFormattingEventArgs e) 48 { 49 // Set the background to red for negative values in the Balance column. 50 if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance")) 51 { 52 Int32 intValue; 53 if (Int32.TryParse((String)e.Value, out intValue) && 54 (intValue < 0)) 55 { 56 e.CellStyle.BackColor = Color.Red; 57 e.CellStyle.SelectionBackColor = Color.DarkRed; 58 } 59 } 60 61 // Replace string values in the Priority column with images. 62 if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority")) 63 { 64 // Ensure that the value is a string. 65 String stringValue = e.Value as string; 66 if (stringValue == null) return; 67 68 // Set the cell ToolTip to the text value. 69 DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex]; 70 cell.ToolTipText = stringValue; 71 72 // Replace the string value with the image value. 73 switch (stringValue) 74 { 75 case "high": 76 e.Value = highPriImage; 77 break; 78 case "medium": 79 e.Value = mediumPriImage; 80 break; 81 case "low": 82 e.Value = lowPriImage; 83 break; 84 } 85 } 86 } 87 88 public static void Main() 89 { 90 Application.Run(new Form1()); 91 } 92 93 }
浙公网安备 33010602011771号