微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐