DataGridView控件用法合集(十二)
来源:互联网 发布:计算器编程学习 编辑:程序博客网 时间:2024/06/05 02:48
DataGridView控件用法合集(十二)
DataGridView编辑
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;
}
}
}
- DataGridView控件用法合集(十二)
- DataGridView控件用法合集(十二)
- DataGridView控件用法合集(十二)
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集
- DataGridView控件用法合集(一)
- DataGridView控件用法合集(二)
- DataGridView控件用法合集(三)
- DataGridView控件用法合集(十一)
- 关于使用XFire...
- Struts_DispatchAction简单的例子
- C#高级编程学习笔记--------构造函数(1)
- 使用dom4j从XML配置文件中生成菜单(一)
- DataGridView控件用法合集(十二)
- apache主配置文件配置解说
- 设计模式一句话解释
- ACCESS专业博客
- 我在hfrk2410上面的linux系统。先erase以前的文件系统,再烧写后启动过程。
- extern "C" {}
- hibernate中用uniqueResult() 方法能得到一个String 类型的值吗?
- sp_MSforeachtable 与 sp_MSforeachdb
- javascript 精华贴