VS2005下DataGridView 的多种样式列控件
VS2005已经发布好久了,但对DataGridView 的使用,在网上的资料还比较少,DataGridView 无论是美观与功能方面都是DataGrid所不能比的,应该说DataGridView 不是DataGrid的升级,因为DataGridView 使用了一套全新的构建方式,引入了DataGridViewCell ,DataGridViewColumn,DataGridViewRow,等,新的概念,也使编程更容易了,它提供了
DataGridViewTextBoxColumn,DataGridViewImageColumn,DataGridLinkColumn,DataGridViewComboBoxColumn,DataGrid
ViewButtonColumn,DataGridViewCheckBoxColumn,等样式列
但我们常用的远不只这些样式列,所以我不想把所有的样式列都给大家做出来,我只是想抛砖引玉通过我自己做的四个样式列来让大家掌握自定义DataGridView样式列的方法.由于时间仓促功能不是很完善,望大家见谅,好了大家先看看4个控件的效果.
一:DataGridViewMaskedTextBoxColumn(正则表达式样式列)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace WindowsApplication23
{
//Cell编辑类,实现IDataGridViewEditingControl接口,可参照ComboBoxEditingControl的写法
public class DataGridViewMaskedTextBoxEditingControl:MaskedTextBox,IDataGridViewEditingControl
{
protected int rowIndex;
protected DataGridView dataGridView;
protected bool valueChanged = false;
public DataGridViewMaskedTextBoxEditingControl()
{
base.TabStop = false;
}
//重写基类(MakedTextBox)的OnTextChanged方法
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
// 当text值发生变化时,通知DataGridView
private void NotifyDataGridViewOfValueChange()
{
valueChanged = true;
dataGridView.NotifyCurrentCellDirty(true);
}
/**//// <summary>
/// 设置对齐方式
/// </summary>
/// <param name="align"></param>
/// <returns></returns>
private static HorizontalAlignment translateAlignment(DataGridViewContentAlignment align)
{
switch (align)
{
case DataGridViewContentAlignment.TopLeft:
case DataGridViewContentAlignment.MiddleLeft:
case DataGridViewContentAlignment.BottomLeft:
return HorizontalAlignment.Left;
case DataGridViewContentAlignment.TopCenter:
case DataGridViewContentAlignment.MiddleCenter:
case DataGridViewContentAlignment.BottomCenter:
return HorizontalAlignment.Center;
case DataGridViewContentAlignment.TopRight:
case DataGridViewContentAlignment.MiddleRight:
case DataGridViewContentAlignment.BottomRight:
return HorizontalAlignment.Right;
}
return HorizontalAlignment.Left;
}
/**//// <summary>
/// 在Cell被编辑的时候光标显示
/// </summary>
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
/**//// <summary>
/// 获取或设置所在的DataGridView
/// </summary>
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
/**//// <summary>
/// 获取或设置格式化后的值
/// </summary>
public object EditingControlFormattedValue
{
set
{
Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
/**//// <summary>
/// 获取控件的Text值
/// </summary>
/// <param name="context">错误上下文</param>
/// <returns></returns>
public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return Text;
}
/**//// <summary>
/// 编辑键盘
/// </summary>
/// <param name="keyData"></param>
/// <param name="dataGridViewWantsInputKey"></param>
/// <returns></returns>
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Right:
if (!(this.SelectionLength == 0
&& this.SelectionStart == this.ToString().Length))
{
return true;
}
break;
case Keys.Left:
if (!(this.SelectionLength == 0
&& this.SelectionStart == 0))
{
return true;
}
break;
case Keys.Home:
case Keys.End:
if (this.SelectionLength != this.ToString().Length)
{
return true;
}
break;
case Keys.Prior:
case Keys.Next:
if (this.valueChanged)
{
return true;
}
break;
case Keys.Delete:
if (this.SelectionLength > 0 || this.SelectionStart < this.ToString().Length)
{
return true;
}
break;
}
return !dataGridViewWantsInputKey;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
if (selectAll)
{
SelectAll();
}
else
{
this.SelectionStart = this.ToString().Length;
}
}
public virtual bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
/**//// <summary>
/// 控件所在行
/// </summary>
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
/**//// <summary>
/// 设置样式
/// </summary>
/// <param name="dataGridViewCellStyle"></param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
}
/**//// <summary>
/// 是否值发生了变化
/// </summary>
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
}
}
(二)DataGridViewFormat(可以获取格式的列,如N2 ,1234,11)
(三)DataGridViewTreeViewColumn(下拉列表树控件)
(四)DataGridViewDateTimeColumn(日期选择列控件)
控件源码下载
/Files/CodeAnyWhere/WindowsApplication23.rar
来自:http://codeanywhere.cnblogs.com