VS2005下DataGridView 的多种样式列控件

VS2005已经发布好久了,但对DataGridView 的使用,在网上的资料还比较少,DataGridView 无论是美观与功能方面都是DataGrid所不能比的,应该说DataGridView 不是DataGrid的升级,因为DataGridView 使用了一套全新的构建方式,引入了DataGridViewCell ,DataGridViewColumn,DataGridViewRow,等,新的概念,也使编程更容易了,它提供了
DataGridViewTextBoxColumn,DataGridViewImageColumn,DataGridLinkColumn,DataGridViewComboBoxColumn,DataGrid
ViewButtonColumn,DataGridViewCheckBoxColumn,等样式列
但我们常用的远不只这些样式列,所以我不想把所有的样式列都给大家做出来,我只是想抛砖引玉通过我自己做的四个样式列来让大家掌握自定义DataGridView样式列的方法.由于时间仓促功能不是很完善,望大家见谅,好了大家先看看4个控件的效果.

一:DataGridViewMaskedTextBoxColumn(正则表达式样式列)
MaskedTextBoxColumn.JPG

ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Drawing;
None.gif
using System.Data;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Drawing.Design;
None.gif
None.gif
namespace WindowsApplication23
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
//Cell编辑类,实现IDataGridViewEditingControl接口,可参照ComboBoxEditingControl的写法
InBlock.gif
    public class DataGridViewMaskedTextBoxEditingControl:MaskedTextBox,IDataGridViewEditingControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected int rowIndex;
InBlock.gif        
protected DataGridView dataGridView;
InBlock.gif        
protected bool valueChanged = false;
InBlock.gif
InBlock.gif        
public DataGridViewMaskedTextBoxEditingControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.TabStop = false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//重写基类(MakedTextBox)的OnTextChanged方法
InBlock.gif
        protected override void OnTextChanged(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnTextChanged(e);
InBlock.gif            NotifyDataGridViewOfValueChange();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//  当text值发生变化时,通知DataGridView
InBlock.gif
        private void NotifyDataGridViewOfValueChange()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            valueChanged 
= true;
InBlock.gif            dataGridView.NotifyCurrentCellDirty(
true);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置对齐方式
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="align"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static HorizontalAlignment translateAlignment(DataGridViewContentAlignment align)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (align)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case DataGridViewContentAlignment.TopLeft:
InBlock.gif                
case DataGridViewContentAlignment.MiddleLeft:
InBlock.gif                
case DataGridViewContentAlignment.BottomLeft:
InBlock.gif                    
return HorizontalAlignment.Left;
InBlock.gif
InBlock.gif                
case DataGridViewContentAlignment.TopCenter:
InBlock.gif                
case DataGridViewContentAlignment.MiddleCenter:
InBlock.gif                
case DataGridViewContentAlignment.BottomCenter:
InBlock.gif                    
return HorizontalAlignment.Center;
InBlock.gif
InBlock.gif                
case DataGridViewContentAlignment.TopRight:
InBlock.gif                
case DataGridViewContentAlignment.MiddleRight:
InBlock.gif                
case DataGridViewContentAlignment.BottomRight:
InBlock.gif                    
return HorizontalAlignment.Right;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif             
return HorizontalAlignment.Left;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在Cell被编辑的时候光标显示
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Cursor EditingPanelCursor
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Cursors.IBeam;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置所在的DataGridView
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DataGridView EditingControlDataGridView
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return dataGridView;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dataGridView 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置格式化后的值
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public object EditingControlFormattedValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Text 
= value.ToString();
InBlock.gif                NotifyDataGridViewOfValueChange();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取控件的Text值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="context">错误上下文</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Text;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 编辑键盘
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="keyData"></param>
InBlock.gif        
/// <param name="dataGridViewWantsInputKey"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (keyData & Keys.KeyCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case Keys.Right:
InBlock.gif                    
if (!(this.SelectionLength == 0
InBlock.gif                          
&& this.SelectionStart == this.ToString().Length))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case Keys.Left:
InBlock.gif                    
if (!(this.SelectionLength == 0
InBlock.gif                          
&& this.SelectionStart == 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case Keys.Home:
InBlock.gif                
case Keys.End:
InBlock.gif                    
if (this.SelectionLength != this.ToString().Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case Keys.Prior:
InBlock.gif                
case Keys.Next:
InBlock.gif                    
if (this.valueChanged)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif
InBlock.gif                
case Keys.Delete:
InBlock.gif                    
if (this.SelectionLength > 0 || this.SelectionStart < this.ToString().Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return !dataGridViewWantsInputKey;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void PrepareEditingControlForEdit(bool selectAll)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (selectAll)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SelectAll();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.SelectionStart = this.ToString().Length;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public virtual bool RepositionEditingControlOnValueChange
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 控件所在行
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int EditingControlRowIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.rowIndex;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.rowIndex = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置样式
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="dataGridViewCellStyle"></param>

InBlock.gif        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Font = dataGridViewCellStyle.Font;
InBlock.gif            
this.ForeColor = dataGridViewCellStyle.ForeColor;
InBlock.gif            
this.BackColor = dataGridViewCellStyle.BackColor;
InBlock.gif            
this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 是否值发生了变化
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool EditingControlValueChanged
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return valueChanged;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.valueChanged = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


(二)DataGridViewFormat(可以获取格式的列,如N2 ,1234,11)FormatColumn.JPG
(三)DataGridViewTreeViewColumn(下拉列表树控件)
ComboBoxTreeViewColumn.JPG
(四)DataGridViewDateTimeColumn(日期选择列控件)
DateTimeColumn.JPG
控件源码下载
/Files/CodeAnyWhere/WindowsApplication23.rar


来自:http://codeanywhere.cnblogs.com

转载于:https://www.cnblogs.com/slcfhr/archive/2006/07/18/453938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值