DataGridView控件用法合集(十二)
近期将DataGridView常用的一些用法做了一个整理。为防止页面过长,现分批贴出来,此为第十二部分。
DataGridView编辑
59. DataGridView中Enter键按下焦点移至旁边的单元格
60. DataGridView行集合化(Group) 59. DataGridView中Enter键按下焦点移至旁边的单元格 [VB.NET] Imports System Imports System.Windows.Forms ''' <summary> ''' Enterキーが押された時に、Tabキーが押されたのと同じ動作をする ''' (現在のセルを隣のセルに移動する)DataGridView ''' </summary> Public Class DataGridViewEx Inherits DataGridView Protected Overrides Function ProcessDialogKey( _ ByVal keyData As Keys) As Boolean 'Enterキーが押された時は、Tabキーが押されたようにする If (keyData And Keys.KeyCode) = Keys.Enter Then Return Me.ProcessTabKey(keyData) End If Return MyBase.ProcessDialogKey(keyData) End Function Protected Overrides Function ProcessDataGridViewKey( _ ByVal e As KeyEventArgs) As Boolean 'Enterキーが押された時は、Tabキーが押されたようにする If e.KeyCode = Keys.Enter Then Return Me.ProcessTabKey(e.KeyCode) End If Return MyBase.ProcessDataGridViewKey(e) End Function End Class [C#] using System; using System.Windows.Forms; /// <summary> /// Enterキーが押された時に、Tabキーが押されたのと同じ動作をする /// (現在のセルを隣のセルに移動する)DataGridView /// </summary> public class DataGridViewEx : DataGridView { protected override bool ProcessDialogKey(Keys keyData) { //Enterキーが押された時は、Tabキーが押されたようにする if ((keyData & Keys.KeyCode) == Keys.Enter) { return this.ProcessTabKey(keyData); } return base.ProcessDialogKey(keyData); } protected override bool ProcessDataGridViewKey(KeyEventArgs e) { //Enterキーが押された時は、Tabキーが押されたようにする if (e.KeyCode == Keys.Enter) { return this.ProcessTabKey(e.KeyCode); } return base.ProcessDataGridViewKey(e); } } 60. DataGridView行集合化(Group) [VB.NET] 'デフォルトのセルスタイル Private defaultCellStyle As DataGridViewCellStyle 'グループ化された一番上の行のセルスタイル Private groupCellStyle 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.groupCellStyle = New DataGridViewCellStyle() Me.groupCellStyle.ForeColor = Color.White Me.groupCellStyle.BackColor = Color.DarkGreen Me.groupCellStyle.SelectionBackColor = Color.DarkBlue End Sub 'CellFormattingイベントハンドラ Private Sub DataGridView1_CellFormatting(ByVal sender As Object,_ ByVal e As DataGridViewCellFormattingEventArgs) _ Handles DataGridView1.CellFormatting Dim dgv As DataGridView = CType(sender,DataGridView) 'セルが1列目で、ヘッダーではなく、新しい行でもないとき If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 AndAlso _ e.RowIndex <> dgv.NewRowIndex Then If e.RowIndex = 0 OrElse _ Not dgv(e.ColumnIndex,e.RowIndex - 1).Value.Equals(e.Value) Then '1行目か、上のセルと違う値の時は、背景色を変更する dgv.Rows(e.RowIndex).DefaultCellStyle = Me.groupCellStyle Else dgv.Rows(e.RowIndex).DefaultCellStyle = Me.defaultCellStyle e.Value = "" e.FormattingApplied = True End If End If End Sub [C#] //デフォルトのセルスタイル private DataGridViewCellStyle defaultCellStyle; //グループ化された一番上の行のセルスタイル private DataGridViewCellStyle groupCellStyle; //フォームのLoadイベントハンドラ private void Form1_Load(object sender,EventArgs e) { //セルスタイルを設定する this.defaultCellStyle = new DataGridViewCellStyle(); this.groupCellStyle = new DataGridViewCellStyle(); this.groupCellStyle.ForeColor = Color.White; this.groupCellStyle.BackColor = Color.DarkGreen; this.groupCellStyle.SelectionBackColor = Color.DarkBlue; } //CellFormattingイベントハンドラ private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { DataGridView dgv = (DataGridView)sender; //セルが1列目で、ヘッダーではなく、新しい行でもないとき if (e.ColumnIndex == 0 && e.RowIndex >= 0 && e.RowIndex != dgv.NewRowIndex) { if (e.RowIndex == 0 || !dgv[e.ColumnIndex,e.RowIndex - 1].Value.Equals(e.Value)) { //1行目か、上のセルと違う値の時は、背景色を変更する dgv.Rows[e.RowIndex].DefaultCellStyle = this.groupCellStyle; } else { dgv.Rows[e.RowIndex].DefaultCellStyle = this.defaultCellStyle; e.Value = ""; e.FormattingApplied = true; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |