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; 样式的优先顺序 一般单元格的样式优先顺位
表头部的样式优先顺位
下例说明 [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; }
单元格的设定 [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的参数一览(整数)
Format的参数一览(小数)
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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |