DataGridView控件用法合集(七)

前端之家收集整理的这篇文章主要介绍了DataGridView控件用法合集(七)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
近期将DataGridView常用的一些用法做了一个整理。为防止页面过长,现分批贴出来,此为第七部分。

DataGridView样式

33. DataGridView单元格样式设置
34. DataGridView文字表示位置的设定
35. DataGridView单元格内文字列换行
36. DataGridView单元格DBNull值表示的设定
37. DataGridView单元格样式格式化
38. DataGridView指定单元格颜色设定
39. DataGridView单元格文字字体设置
40. DataGridView根据单元格值设定单元格样式

33. DataGridView单元格样式设置

指定行列的样式设定

[VB.NET]

'インデックス0の列のセルの背景色を水色にする

DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Aqua

'インデックス0の行のセルの背景色を薄い灰色にする

DataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.LightGray

[C#]

//インデックス0の列のセルの背景色を水色にする

DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;

//インデックス0の行のセルの背景色を薄い灰色にする

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
  7. @H_301_100@

    表头部的样式优先顺位

    1. DataGridViewCell.Style
    2. DataGridView.RowHeadersDefaultCellStyle
    3. DataGridView.ColumnHeadersDefaultCellStyle
    4. DataGridView.DefaultCellStyle
    5. @H_301_100@

      下例说明

      [VB.NET]

      '1列目を水色にする

      DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Aqua

      '全ての列の背景色を黄色にする

      DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow

      '奇数行を黄緑色にする

      DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow

      '3行目をピンクにする

      DataGridView1.Rows(2).DefaultCellStyle.BackColor = Color.Pink

      '自身のセルスタイルと継承されたセルスタイルの背景色を取得する

      '1列目のセルスタイル

      '"[Aqua]"と"[Aqua]"と表示される

      Console.WriteLine(DataGridView1.Columns(0).DefaultCellStyle.BackColor)

      Console.WriteLine(DataGridView1.Columns(0).InheritedStyle.BackColor)

      '1行目のセルスタイル

      '"[Empty]"と"[Yellow]"と表示される

      Console.WriteLine(DataGridView1.Rows(0).DefaultCellStyle.BackColor)

      Console.WriteLine(DataGridView1.Rows(0).InheritedStyle.BackColor)

      '2行目のセルスタイル

      '"[Empty]"と"[GreenYellow]"と表示される

      Console.WriteLine(DataGridView1.Rows(1).DefaultCellStyle.BackColor)

      Console.WriteLine(DataGridView1.Rows(1).InheritedStyle.BackColor)

      '3行目のセルスタイル

      '"[Pink]"と"[Pink]"と表示される

      Console.WriteLine(DataGridView1.Rows(2).DefaultCellStyle.BackColor)

      Console.WriteLine(DataGridView1.Rows(2).InheritedStyle.BackColor)

      '(0,3)のセルスタイル

      '"[Empty]"と"[Pink]"と表示される

      Console.WriteLine(DataGridView1(0,2).Style.BackColor)

      Console.WriteLine(DataGridView1(0,2).InheritedStyle.BackColor)

      [C#]

      //1列目を水色にする

      DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;

      //全ての列の背景色を黄色にする

      DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow;

      //奇数行を黄緑色にする

      DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;

      //3行目をピンクにする

      DataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Pink;

      //自身のセルスタイルと継承されたセルスタイルの背景色を取得する

      //1列目のセルスタイル

      //"[Aqua]"と"[Aqua]"と表示される

      Console.WriteLine(DataGridView1.Columns[0].DefaultCellStyle.BackColor);

      Console.WriteLine(DataGridView1.Columns[0].InheritedStyle.BackColor);

      //1行目のセルスタイル

      //"[Empty]"と"[Yellow]"と表示される

      Console.WriteLine(DataGridView1.Rows[0].DefaultCellStyle.BackColor);

      Console.WriteLine(DataGridView1.Rows[0].InheritedStyle.BackColor);

      //2行目のセルスタイル

      //"[Empty]"と"[GreenYellow]"と表示される

      Console.WriteLine(DataGridView1.Rows[1].DefaultCellStyle.BackColor);

      Console.WriteLine(DataGridView1.Rows[1].InheritedStyle.BackColor);

      //3行目のセルスタイル

      //"[Pink]"と"[Pink]"と表示される

      Console.WriteLine(DataGridView1.Rows[2].DefaultCellStyle.BackColor);

      Console.WriteLine(DataGridView1.Rows[2].InheritedStyle.BackColor);

      //(0,3)のセルスタイル

      //"[Empty]"と"[Pink]"と表示される

      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]

      '"Column1"列のセルのテキストの配置を上下左右とも中央にする

      DataGridView1.Columns("Column1").DefaultCellStyle.Alignment = _

      DataGridViewContentAlignment.MiddleCenter

      [C#]

      //"Column1"列のセルのテキストの配置を上下左右とも中央にする

      DataGridView1.Columns["Column1"].DefaultCellStyle.Alignment =

      DataGridViewContentAlignment.MiddleCenter;

      表头的设定

      [VB.NET]

      '"Column1"列のヘッダーのテキストの配置を上下左右とも中央にする

      DataGridView1.Columns("Column1").HeaderCell.Style.Alignment = _

      DataGridViewContentAlignment.MiddleCenter

      [C#]

      //"Column1"列のヘッダーのテキストの配置を上下左右とも中央にする

      DataGridView1.Columns["Column1"].HeaderCell.Style.Alignment =

      DataGridViewContentAlignment.MiddleCenter;

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

      [VB.NET]

      '"Column1"列のセルのテキストを折り返して表示する

      DataGridView1.Columns("Column1").DefaultCellStyle.WrapMode = _

      DataGridViewTriState.True

      'ヘッダーも折り返して表示するなら、次のようにする

      DataGridView1.Columns("Column1").HeaderCell.Style.WrapMode = _

      DataGridViewTriState.True

      [C#]

      //"Column1"列のセルのテキストを折り返して表示する

      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"

      '2列目のカルチャを変更する

      DataGridView1.Columns(1).DefaultCellStyle.FormatProvider = _

      New System.Globalization.CultureInfo("en-US")

      [C#]

      //列のセルのテキストの書式を地域通貨として指定する

      DataGridView1.Columns[0].DefaultCellStyle.Format = "c";

      DataGridView1.Columns[1].DefaultCellStyle.Format = "c";

      //2列目のカルチャを変更する

      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

      iの値は「#」です。

      iの値は「123456」です

      Format的参数一览(小数)

      書式

      説明

      値が"1.23456789"の時

      書式なし

      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]

      'DataGridView1のCellMouseEnterイベントハンドラ

      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

      'DataGridView1のCellMouseLeaveイベントハンドラ

      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)

      'セルスタイルを元に戻す

      'セルスタイルを削除するなら、nullを設定してもよい

      dgv(e.ColumnIndex,e.RowIndex).Style.BackColor = Color.Empty

      dgv(e.ColumnIndex,e.RowIndex).Style.SelectionBackColor = Color.Empty

      End If

      End Sub

      [C#]

      //DataGridView1のCellMouseEnterイベントハンドラ

      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;

      }

      }

      //DataGridView1のCellMouseLeaveイベントハンドラ

      private void DataGridView1_CellMouseLeave(object sender,

      DataGridViewCellEventArgs e)

      {

      //ヘッダー以外のセル

      if (e.ColumnIndex >= 0 && e.RowIndex >= 0)

      {

      DataGridView dgv = (DataGridView)sender;

      //セルスタイルを元に戻す

      //セルスタイルを削除するなら、nullを設定してもよい

      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

      'フォームのLoadイベントハンドラ

      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

      'DataGridView1のCellMouseEnterイベントハンドラ

      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

      'DataGridView1のCellMouseLeaveイベントハンドラ

      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)

      'セルスタイルを元に戻す

      'セルスタイルを削除するなら、nullを設定してもよい

      dgv(e.ColumnIndex,e.RowIndex).Style = Me.defaultCellStyle

      End If

      End Sub

      [C#]

      //デフォルトのセルスタイル

      private DataGridViewCellStyle defaultCellStyle;

      //マウスポインタの下にあるセルのセルスタイル

      private DataGridViewCellStyle mouseCellStyle;

      //フォームのLoadイベントハンドラ

      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);

      }

      //DataGridView1のCellEnterイベントハンドラ

      private void DataGridView1_CellEnter(object sender,e.RowIndex].Style = this.mouseCellStyle;

      }

      }

      //DataGridView1のCellLeaveイベントハンドラ

      private void DataGridView1_CellLeave(object sender,e.RowIndex].Style = this.defaultCellStyle;

      }

      }

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

      单元格负数情况下显示黄色,0的情况下显示红色

      [VB.NET]

      'CellFormattingイベントハンドラ

      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#]

      //CellFormattingイベントハンドラ

      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;

      }

      }

      }

猜你在找的VB相关文章