目录
36. DataGridView单元格DBNull值表示的设定
33. DataGridView单元格样式设置
指定行列的样式设定
[VB.NET]
DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Aqua
DataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.LightGray
[C#]
DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;
DataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;
奇数行样式设定
[VB.NET]
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow
[C#]
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;
行,列表头部的样式设定
[VB.NET]
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Ivory
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Lime
[C#]
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Ivory;
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Lime;
样式的优先顺序
一般单元格的样式优先顺位
- DataGridViewCell.Style
- DataGridViewRow.DefaultCellStyle
- DataGridView.AlternatingRowsDefaultCellStyle
- DataGridView.RowsDefaultCellStyle
- DataGridViewColumn.DefaultCellStyle
- DataGridView.DefaultCellStyle
表头部的样式优先顺位
- DataGridViewCell.Style
- DataGridView.RowHeadersDefaultCellStyle
- DataGridView.ColumnHeadersDefaultCellStyle
- DataGridView.DefaultCellStyle
下例说明
[VB.NET]
DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Aqua
DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow
DataGridView1.Rows(2).DefaultCellStyle.BackColor = Color.Pink
Console.WriteLine(DataGridView1.Columns(0).DefaultCellStyle.BackColor)
Console.WriteLine(DataGridView1.Columns(0).InheritedStyle.BackColor)
Console.WriteLine(DataGridView1.Rows(0).DefaultCellStyle.BackColor)
Console.WriteLine(DataGridView1.Rows(0).InheritedStyle.BackColor)
Console.WriteLine(DataGridView1.Rows(1).DefaultCellStyle.BackColor)
Console.WriteLine(DataGridView1.Rows(1).InheritedStyle.BackColor)
Console.WriteLine(DataGridView1.Rows(2).DefaultCellStyle.BackColor)
Console.WriteLine(DataGridView1.Rows(2).InheritedStyle.BackColor)
Console.WriteLine(DataGridView1(0, 2).Style.BackColor)
Console.WriteLine(DataGridView1(0, 2).InheritedStyle.BackColor)
[C#]
DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;
DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow;
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;
DataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Pink;
Console.WriteLine(DataGridView1.Columns[0].DefaultCellStyle.BackColor);
Console.WriteLine(DataGridView1.Columns[0].InheritedStyle.BackColor);
Console.WriteLine(DataGridView1.Rows[0].DefaultCellStyle.BackColor);
Console.WriteLine(DataGridView1.Rows[0].InheritedStyle.BackColor);
Console.WriteLine(DataGridView1.Rows[1].DefaultCellStyle.BackColor);
Console.WriteLine(DataGridView1.Rows[1].InheritedStyle.BackColor);
Console.WriteLine(DataGridView1.Rows[2].DefaultCellStyle.BackColor);
Console.WriteLine(DataGridView1.Rows[2].InheritedStyle.BackColor);
Console.WriteLine(DataGridView1[0, 2].Style.BackColor);
Console.WriteLine(DataGridView1[0, 2].InheritedStyle.BackColor);
复数行列的样式设定
[VB.NET]
Dim cellStyle As New DataGridViewCellStyle()
cellStyle.BackColor = Color.Yellow
For i As Integer = 0 To DataGridView1.Columns.Count - 1
If i Mod 2 = 0 Then
DataGridView1.Columns(i).DefaultCellStyle = cellStyle
End If
Next i
For i As Integer = 0 To DataGridView1.Columns.Count - 1
If i Mod 2 = 0 Then
DataGridView1.Columns(i).DefaultCellStyle.BackColor = Color.Yellow
End If
Next i
[C#]
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
cellStyle.BackColor = Color.Yellow;
for (int i = 0; i < DataGridView1.Columns.Count; i++)
{
if (i % 2 == 0)
DataGridView1.Columns[i].DefaultCellStyle = cellStyle;
}
for (int i = 0; i < DataGridView1.Columns.Count; i++)
{
if (i % 2 == 0)
DataGridView1.Columns[i].DefaultCellStyle.BackColor = Color.Yellow;
}
34. DataGridView文字表示位置的设定
单元格的设定
[VB.NET]
DataGridView1.Columns("Column1").DefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleCenter
[C#]
DataGridView1.Columns["Column1"].DefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleCenter;
表头的设定
[VB.NET]
DataGridView1.Columns("Column1").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
[C#]
DataGridView1.Columns["Column1"].HeaderCell.Style.Alignment =DataGridViewContentAlignment.MiddleCenter;
35. DataGridView单元格内文字列换行
[VB.NET]
DataGridView1.Columns("Column1").DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView1.Columns("Column1").HeaderCell.Style.WrapMode = DataGridViewTriState.True
[C#]
DataGridView1.Columns["Column1"].DefaultCellStyle.WrapMode =DataGridViewTriState.True;
DataGridView1.Columns["Column1"].HeaderCell.Style.WrapMode =DataGridViewTriState.True;
36. DataGridView单元格DBNull值表示的设定
[VB.NET]
DataGridView1.DefaultCellStyle.NullValue = "(没有被指定)"
[C#]
DataGridView1.DefaultCellStyle.NullValue = "(没有被指定。)";
单元格内NullValue属性设定的值输入,表示单元格内为Null值
[VB.NET]
DataGridView1.DefaultCellStyle.NullValue = "-"
DataGridView1.DefaultCellStyle.DataSourceNullValue = "X"
[C#]
DataGridView1.DefaultCellStyle.NullValue = "-";
DataGridView1.DefaultCellStyle.DataSourceNullValue = "X";
37. DataGridView单元格样式格式化
[VB.NET]
DataGridView1.Columns(0).DefaultCellStyle.Format = "c"
DataGridView1.Columns(1).DefaultCellStyle.Format = "c"
DataGridView1.Columns(1).DefaultCellStyle.FormatProvider = New System.Globalization.CultureInfo("en-US")
[C#]
DataGridView1.Columns[0].DefaultCellStyle.Format = "c";
DataGridView1.Columns[1].DefaultCellStyle.Format = "c";
DataGridView1.Columns[1].DefaultCellStyle.FormatProvider =new System.Globalization.CultureInfo("en-US");
Format的参数一览(整数)
格式 | 说明 | 当值为“123456”时 |
没有格式 | 123456 | |
C | 通貨 | /123,456 |
D | 10進数 | 123456 |
E | 指数 | 1.234560E+005 |
F | 固定小数点 | 123456.00 |
G | 一般 | 123456 |
N | 数値 | 123,456.00 |
P | 百分数 | 12,345,600.00% |
R | 回次 | (出现错误) |
X | 16進数 | 1E240 |
0 | 123456 | |
00000000 | 00123456 | |
######## | 123456 | |
#,##0 | 123,456 | |
%0 | %12345600 | |
00.000E0 | 12.346E4 | |
加#;减#;零 | 加123456 |
Format的参数一览(小数)
格式 | 説明 | 当值为“123456”时 |
没有格式 | 1.23456789 | |
C | 通貨 | /1 |
D | 10進数 | (出现错误) |
E | 指数 | 1.234568E+000 |
F | 固定小数点 | 1.23 |
G | 一般 | 1.23456789 |
N | 数値 | 1.23 |
P | パーセント | 123.46% |
R | ラウンドトリップ | 1.23456789 |
X | 16進数 | (出现错误) |
00.0000000000 | 01.2345678900 | |
##.########## | 1.23456789 | |
#,##0.000 | 1.235 | |
%0.## | %123.46 | |
00.000E0 | 12.346E-1 | |
加#;减#;零 | 加1.23 | |
d的值是#.## | d的值是“1.23” |
38. DataGridView指定单元格颜色设定
光标下的单元格颜色自动变换
[VB.NET]
Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellMouseEnter
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
dgv(e.ColumnIndex, e.RowIndex).Style.SelectionBackColor = Color.Red
End If
End Sub
Private Sub DataGridView1_CellMouseLeave(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellMouseLeave
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Empty
dgv(e.ColumnIndex, e.RowIndex).Style.SelectionBackColor = Color.Empty
End If
End Sub
[C#]
private void DataGridView1_CellMouseEnter(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Red;
}
}
private void DataGridView1_CellMouseLeave(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Empty;
dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Empty;
}
}
表头部单元格颜色设定
[VB.NET]
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.YellowGreen
DataGridView1.TopLeftHeaderCell.Style.BackColor = Color.Blue
[C#]
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow;
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.YellowGreen;
DataGridView1.TopLeftHeaderCell.Style.BackColor = Color.Blue;
39. DataGridView单元格文字字体设置
光标下单元格字体设置为粗体
[VB.NET]
Private defaultCellStyle As DataGridViewCellStyle
Private mouseCellStyle As DataGridViewCellStyle
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.defaultCellStyle = New DataGridViewCellStyle()
Me.mouseCellStyle = New DataGridViewCellStyle()
Me.mouseCellStyle.Font = New Font(DataGridView1.Font, _
DataGridView1.Font.Style Or FontStyle.Bold)
End Sub
Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellMouseEnter
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style = Me.mouseCellStyle
End If
End Sub
Private Sub DataGridView1_CellMouseLeave(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellMouseLeave
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style = Me.defaultCellStyle
End If
End Sub
[C#]
private DataGridViewCellStyle defaultCellStyle;
private DataGridViewCellStyle mouseCellStyle;
private void Form1_Load(object sender, EventArgs e)
{
this.defaultCellStyle = new DataGridViewCellStyle();
this.mouseCellStyle = new DataGridViewCellStyle();
this.mouseCellStyle.Font = new Font(DataGridView1.Font,
DataGridView1.Font.Style | FontStyle.Bold);
}
private void DataGridView1_CellEnter(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style = this.mouseCellStyle;
}
}
private void DataGridView1_CellLeave(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style = this.defaultCellStyle;
}
}
40. DataGridView根据单元格值设定单元格样式
单元格负数情况下显示黄色,0的情况下显示红色
[VB.NET]
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
TypeOf e.Value Is Integer Then
Dim val As Integer = CInt(e.Value)
If val < 0 Then
e.CellStyle.BackColor = Color.Yellow
Else If val = 0 Then
e.CellStyle.BackColor = Color.Red
End If
End If
End Sub
[C#]
private void DataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is int)
{
int val = (int)e.Value;
if (val < 0)
{
e.CellStyle.BackColor = Color.Yellow;
}
else if (val == 0)
{
e.CellStyle.BackColor = Color.Red;
}
}
}
欢迎微信技术交流: