DataGridView用法合集(7):样式

目录

33. DataGridView单元格样式设置

34. DataGridView文字表示位置的设定

35. DataGridView单元格内文字列换行

36. DataGridView单元格DBNull值表示的设定

37. DataGridView单元格样式格式化

38. DataGridView指定单元格颜色设定

39. DataGridView单元格文字字体设置

40. DataGridView根据单元格值设定单元格样式


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;

样式的优先顺序

           一般单元格的样式优先顺位

  1. DataGridViewCell.Style
  2. DataGridViewRow.DefaultCellStyle
  3. DataGridView.AlternatingRowsDefaultCellStyle
  4. DataGridView.RowsDefaultCellStyle
  5. DataGridViewColumn.DefaultCellStyle
  6. DataGridView.DefaultCellStyle

表头部的样式优先顺位

  1. DataGridViewCell.Style
  2. DataGridView.RowHeadersDefaultCellStyle
  3. DataGridView.ColumnHeadersDefaultCellStyle
  4. 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;
        }
    }
}

 欢迎微信技术交流:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xwLink1996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值