下面的代码示例演示如何实现一个 事件的处理程序,该处理程序根据单元格的列和值更改单元格的显示方式。

Balance 列中包含负数的单元格被指定为红色背景。也可以将这些单元格的格式设置为货币格式以在负值两边显示圆括号。有关更多信息,请参见如何:设置 Windows 窗体 DataGridView 控件中的数据格式

Priority 列的单元格显示代替对应的文本单元格值的图像。 属性既用于获取文本单元格值,也用于设置对应的图像显示值。

来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置using System;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置using 
System.Drawing;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置using 
System.Windows.Forms;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置public class Form1 
: Form
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置{
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    private DataGridView dataGridView1 
= new DataGridView();
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    private Bitmap highPriImage;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    private Bitmap mediumPriImage;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    private Bitmap lowPriImage;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    public Form1()
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
// Initialize the images. 
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        try
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            highPriImage 
= new Bitmap("highPri.bmp");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            mediumPriImage 
= new Bitmap("mediumPri.bmp");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            lowPriImage 
= new Bitmap("lowPri.bmp");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        catch (ArgumentException)
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            MessageBox
.Show("The Priority column requires Bitmap images " +
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                
"residing in the same directory as the executable file.");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
// Initialize the DataGridView.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Dock = DockStyle.Fill;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.AllowUserToAddRows = false;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Columns.AddRange(
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            new DataGridViewTextBoxColumn()
,
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            new DataGridViewImageColumn());
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Columns[0].Name = "Balance";
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Columns[1].Name = "Priority";
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Rows.Add("-100", "high");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Rows.Add("0", "medium");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.Rows.Add("100", "low");
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        dataGridView1
.CellFormatting +=
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            new 
System.Windows.Forms.DataGridViewCellFormattingEventHandler(
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            this
.dataGridView1_CellFormatting);
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        this
.Controls.Add(dataGridView1);
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    
// Changes how cells are displayed depending on their columns and values.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    private void dataGridView1_CellFormatting(object sender
, 
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
// Set the background to red for negative values in the Balance column.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            Int32 intValue;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            
if (Int32.TryParse((String)e.Value, out intValue) && 
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                (intValue 
< 0))
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                e
.CellStyle.BackColor = Color.Red;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                e
.CellStyle.SelectionBackColor = Color.DarkRed;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
// Replace string values in the Priority column with images.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            
// Ensure that the value is a string.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            String stringValue 
= e.Value as string;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            
if (stringValue == null) return;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            
// Set the cell ToolTip to the text value.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            DataGridViewCell cell 
= dataGridView1[e.ColumnIndex, e.RowIndex];
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            cell
.ToolTipText = stringValue;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            
// Replace the string value with the image value.
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            switch (stringValue)
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                case 
"high":
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                    e
.Value = highPriImage;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                    break;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                case 
"medium":
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                    e
.Value = mediumPriImage;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                    break;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                case 
"low":
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                    e
.Value = lowPriImage;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置                    break;
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置            }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    public static void Main()
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    {
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置        Application
.Run(new Form1());
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置    }
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置}
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置

 

相关文章: