来自MSDN的一个Sample:如何自定义 Windows 窗体 DataGridView 控件中的数据格式设置
2007-04-24 21:25
1061 查看
下面的代码示例演示如何实现一个 System.Windows.Forms.DataGridView.CellFormatting 事件的处理程序,该处理程序根据单元格的列和值更改单元格的显示方式。
using System;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
public Form1()
{
// Initialize the images.
try
{
highPriImage = new Bitmap("highPri.bmp");
mediumPriImage = new Bitmap("mediumPri.bmp");
lowPriImage = new Bitmap("lowPri.bmp");
}
catch (ArgumentException)
{
MessageBox.Show("The Priority column requires Bitmap images " +
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
"residing in the same directory as the executable file.");
}
// Initialize the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.AddRange(
new DataGridViewTextBoxColumn(),
new DataGridViewImageColumn());
dataGridView1.Columns[0].Name = "Balance";
dataGridView1.Columns[1].Name = "Priority";
dataGridView1.Rows.Add("-100", "high");
dataGridView1.Rows.Add("0", "medium");
dataGridView1.Rows.Add("100", "low");
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
this.Controls.Add(dataGridView1);
}
// Changes how cells are displayed depending on their columns and values.
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText = stringValue;
// Replace the string value with the image value.
switch (stringValue)
{
case "high":
e.Value = highPriImage;
break;
case "medium":
e.Value = mediumPriImage;
break;
case "low":
e.Value = lowPriImage;
break;
}
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
Balance列中包含负数的单元格被指定为红色背景。也可以将这些单元格的格式设置为货币格式以在负值两边显示圆括号。有关更多信息,请参见如何:设置 Windows 窗体 DataGridView 控件中的数据格式。
Priority列的单元格显示代替对应的文本单元格值的图像。DataGridViewCellFormattingEventArgs 的 Value 属性既用于获取文本单元格值,也用于设置对应的图像显示值。
using System;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
private Bitmap highPriImage;
private Bitmap mediumPriImage;
private Bitmap lowPriImage;
public Form1()
{
// Initialize the images.
try
{
highPriImage = new Bitmap("highPri.bmp");
mediumPriImage = new Bitmap("mediumPri.bmp");
lowPriImage = new Bitmap("lowPri.bmp");
}
catch (ArgumentException)
{
MessageBox.Show("The Priority column requires Bitmap images " +
"named highPri.bmp, mediumPri.bmp, and lowPri.bmp " +
"residing in the same directory as the executable file.");
}
// Initialize the DataGridView.
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.AddRange(
new DataGridViewTextBoxColumn(),
new DataGridViewImageColumn());
dataGridView1.Columns[0].Name = "Balance";
dataGridView1.Columns[1].Name = "Priority";
dataGridView1.Rows.Add("-100", "high");
dataGridView1.Rows.Add("0", "medium");
dataGridView1.Rows.Add("100", "low");
dataGridView1.CellFormatting +=
new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
this.dataGridView1_CellFormatting);
this.Controls.Add(dataGridView1);
}
// Changes how cells are displayed depending on their columns and values.
private void dataGridView1_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// Set the background to red for negative values in the Balance column.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
// Replace string values in the Priority column with images.
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
{
// Ensure that the value is a string.
String stringValue = e.Value as string;
if (stringValue == null) return;
// Set the cell ToolTip to the text value.
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
cell.ToolTipText = stringValue;
// Replace the string value with the image value.
switch (stringValue)
{
case "high":
e.Value = highPriImage;
break;
case "medium":
e.Value = mediumPriImage;
break;
case "low":
e.Value = lowPriImage;
break;
}
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
相关文章推荐
- 如何:将数据绑定到 Windows 窗体 DataGridView 控件
- 『转MSDN』如何:在 Windows 窗体 DataGridView 单元格中承载日历控件
- 如何:禁用 Windows 窗体 DataGridView 控件的按钮列中的按钮
- 如何把Windows 窗体 DataGridView 的某一列的数据显示为“*”。
- vc如何自定义消息与在线程中向主窗体控件发送数据
- 使用两个 Windows 窗体 DataGridView 控件创建一个主/从窗体
- 、首先,我们添加一个主界面窗体,命名为MainForm,该窗体IsMdiContainer设置为True,也就是设置为多文档窗体格式。拖拉布局控件"WeifenLuo.WinFormsUI.Docking.DockPanel"到主窗体MainForm中,并
- C# 如何:禁用 Windows 窗体 DataGridView 控件的按钮列中的按钮 Enable = false
- 图形的绘制,如何使用自定义画笔(颜色,线宽,线形)。如何为程序中添加选项菜单和选项设置对话框,如何使用标准颜色对话框,如何使用字体对话框,在选项对话框中实现预览功能。实现选项对话框和窗口类中的数据交换。如何改变对话框和控件的背景色,如何改变控件的文本颜色,
- 如何:在 Windows 窗体 DataGridView 单元格中承载控件
- Flex中如何创建一个自定义数据显示Tips的slider控件。鼠标按住滑标察看效果
- 使用两个 Windows 窗体 DataGridView 控件创建一个主/从窗体
- 如何:操作 Windows 窗体 DataGridView 控件中的列
- 如何:禁用 Windows 窗体 DataGridView 控件的按钮列中的按钮
- 如何:为 Windows 窗体 DataGridView 控件中的新行指定默认值
- 如何把Windows 窗体 DataGridView 的某一列的数据显示为“*”
- Windows下如何自定义窗体控件
- MFC如何自定义消息与在线程中向主窗体控件发送数据
- 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)
- 如何:从 Windows 窗体 DataGridView 控件中移除自动生成的列