当前位置:开发者网络 >> 技术教程 >> .NET教程 >> 数据库应用 >> 内容
精彩推荐
分类最新教程
分类热点教程
  
DataGrid 风格管理类测试版源码
作者:未知
日期:2005-10-22
人气:
投稿:(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:
众所周知,WINForm 的 DataGrid 组件的功能强大而且灵活,作为一个数据库程序离开它可不行,但是dataGrid在设计期间采用“套用式样”的方式设计表格的外观并不够灵活,那有没有办法统一管理表格风格呢?答案是有。
    作为数据表格的应用最重要的应该不是风格的颜色搭配,而是字段属性的格式化,要格式化每一列的数据显示值,比如说:dataGrid的记录集如果有个“日期时间”字段的话,就会发现,默认的话其实它只显示了日期部分,而时间被Format掉了,这样的话不能满足应用的需求。很多新手朋友也发过帖子询问如何显示出时间,本人也是属于新手行列,查阅了MSDN后编写了一个管理dataGrid 格式列和风格的类,它可以实现管理‘列宽、字体、标题名、格式类型、是否隐藏、行选、行只读、列只读’的基本功能。
    先大致讲一下格式化列的原理。列的格式由DataGridTextBoxColumn的Format属性进行控制,Format 属性可以支持日期、货币、数值、文本等格式字符,具体信息可以参考MSDN:http://msdn.microsoft.com/library/CHS/cpref/html/frlrfSystemWindowsFormsDataGridTextBoxColumnClassFormatTopic.asp?frame=true。
    这里演示格式化“日期时间”,代码:
DataGridTextBoxColumn gridColumn = DataGridTextBoxColumn();
gridColumn.Format=System.String.Format("yyyy-MM-dd hh:mm:ss",gridColumn.TextBox); 
      现在我把类的源码贴上面,我不保证代码的一些做法是否合理与正确,仅供参考:   
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing;

namespace Grid.Service {
  
/// <summary>
  
/// 格式化字符串
  
/// </summary>

  public enum FormatIndex{
    Text 
= 0x0,     //文本
    Boolean,        //是/否(钩选框)
    Money,          //货币(¥#0.00)
    Numeric,        //数值格式(#0.00)
    RealNumber,     //实数(0.000000)
    LongDateTime,   //长日期时间(yyyy年mm月dd日 hh:mm:ss)
    DateTime,       //日期时间(yyyy-mm-dd hh:mm:ss)
    LongDate,       //长日期(yyyy-mm-dd)
    ShortDate,      //短日期(yy-mm-dd)
    Time            //时间(hh:mm:ss)
  }
;

  
/// <summary>
  
/// StyleManager 的摘要说明。
  
/// DataGrid 的属性管理类
  
/// </summary>

  public class StyleManager {

    
public static int lastSelectIndex = -1;
    
private DataGrid _dataGrid =null;
    
private DataTable _dataTable =null;
    
private bool _allowSelectedRow =false;
    
private int[] ReadonlyRows = null;
    
private bool isReadonlyRow = false;
    
private bool _dataGridReadonly = false;

    
public StyleManager(DataGrid dataGrid){
      _dataGrid 
= dataGrid;
    }

    
public StyleManager(){;}

    [Category(
"Action")]
    [Description(
"可操作的数据表格")]
    
public DataGrid @DataGrid{
      
get{return _dataGrid;}
      
set{
        _dataGrid
=value;
        
        
this._dataGrid.CurrentCellChanged -= new System.EventHandler(this.dataGrid_CurrentCellChanged);
        
if(this._allowSelectedRow){
          
if(_dataGrid!=null){
            
//修改事件
            this._dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged);
          }

        }

      }

    }

    [Category(
"Data")]
    [Description(
"要与数据表格绑定的数据源")]
    
public DataTable DataSource{
      
get{return _dataTable;}
      
set{_dataTable =(value as DataTable);}
    }

    [Category(
"Appearance")]
    [Description(
"获取当前的表格风格")]
    
public DataGridTableStyle CurrentTableStyle{
      
get{return GetGridTableStyle();}
    }


    
/// <summary>
    
/// 允许行选择模式
    
/// </summary>

    [Category("Behavior")]
    [Description(
"允许行选择模式")]
    
public bool AllowSelectedRow{
      
get{return _allowSelectedRow;}
      
set{
        _allowSelectedRow 
=value;
        
this._dataGrid.CurrentCellChanged -= new System.EventHandler(this.dataGrid_CurrentCellChanged);
        
if(value){
          
if(_dataGrid!=null){
            
//修改事件
            this._dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged);
          }

        }

      }

    }

    [Category(
"Behavior")]
    [Description(
"表格只读,也可隐藏底部的新增行")]
    
public bool ReadOnly{
      
get{return _dataGridReadonly;}
      
set{_dataGridReadonly = value;}
    }

    
/// <summary>
    
/// 行选择事件
    
/// </summary>
    
/// <param name="sender">对象</param>
    
/// <param name="e">事件参数</param>

    private void dataGrid_CurrentCellChanged(object sender, System.EventArgs e) {
    
      
if(this.DataSource!=null){
        
try{
          
this._dataGrid.Select(this._dataGrid.CurrentRowIndex);
          
if(StyleManager.lastSelectIndex!=-1){
            
this._dataGrid.UnSelect(StyleManager.lastSelectIndex);
          }

          StyleManager.lastSelectIndex 
= this._dataGrid.CurrentRowIndex;
        }
catch{;}
      }

      
//锁定表格行的算法
      if(isReadonlyRow&&this.ReadonlyRows!=null){
        
this._dataGrid.ReadOnly = false;
        
if(this.ReadonlyRows.Length>0)
          
for(int i=0;i<ReadonlyRows.Length;i++){
            
if(this._dataGrid.CurrentRowIndex==ReadonlyRows[i]){
              
this._dataGrid.ReadOnly = true;
              
break;
            }

          }

      }

      
if(_dataGridReadonly){
        
this._dataGrid.ReadOnly = true;
        
return;
      }

    }


    
/// <summary>
    
/// 设置列头风格
    
/// </summary>
    
/// <param name="HeaderFont">字体</param>
    
/// <param name="HeaderFontColor">文字颜色</param>
    
/// <returns>是否成功</returns>

    public bool SetHeader(Font HeaderFont,Color HeaderFontColor){
      
try{
        DataGridTableStyle currGTS 
= GetGridTableStyle();
        
if(currGTS==null){
          
throw new Exception("初始化表风格出错!");
        }

        currGTS.HeaderFont 
= HeaderFont;
        currGTS.HeaderForeColor 
= HeaderFontColor;
        
return true;
      }
catch{
        
return false;
      }

    }

    
/// <summary>
    
/// 设置列头风格,默认黑色字体颜色
    
/// </summary>
    
/// <param name="HeaderFont">字体</param>
    
/// <returns>是否成功</returns>

    public bool SetHeader(Font HeaderFont){
      
return SetHeader(HeaderFont,System.Drawing.Color.Black);
    }

    
/// <summary>
    
/// 设置表格风格
    
/// </summary>
    
/// <param name="CaptionText">标题文本</param>
    
/// <param name="CaptionVisible">标题可视</param>
    
/// <param name="RowHeaderVisible">行头可视</param>
    
/// <returns>是否成功</returns>

    public bool SetGrid(string CaptionText,bool CaptionVisible,bool RowHeaderVisible){
      
try{
        DataGridTableStyle currGTS 
= GetGridTableStyle();
        
if(currGTS==null){
          
throw new Exception("初始化表风格出错!");
        }

        
if(this._dataGrid==null){
          
throw new Exception("无可操作的表格对象!");
        }

        
this._dataGrid.CaptionText = CaptionText;
        
this._dataGrid.CaptionVisible = CaptionVisible;
        currGTS.RowHeadersVisible 
= RowHeaderVisible;
        
return true;
      }
catch{
        
return false;
      }

    }


    
/// <summary>
    
/// 根据数据类型获取列类型
    
/// </summary>
    
/// <param name="tp">数据类型</param>
    
/// <returns></returns>

    private int getDataGridExColumn(Type tp){
      
if(tp==System.Type.GetType("System.Boolean")){
        
return 2;
      }
else if(tp==System.Type.GetType("System.DateTime")){
        
return 6;
      }
else if(tp==System.Type.GetType("System.Decimal")){
        
return 1;
      }
else if(tp==System.Type.GetType("System.Single")||tp==System.Type.GetType("System.Double")) {
        
return 3;
      }
else{
        
return 0;
      }

    }

    
/// <summary>
    
/// 获取默认默认风格
    
/// </summary>
    
/// <returns>表风格</returns>

    private DataGridTableStyle GetGridTableStyle(){
      
try{
        
if (_dataGrid.TableStyles.Count<=0&&_dataTable==null)
          
throw new ArgumentNullException("数据源为空!");

        
if((_dataGrid.DataSource as DataTable)!=_dataTable){
          _dataGrid.DataSource 
= _dataTable;
        }


        
if(_dataGrid.TableStyles.Count<=0){
          DataGridTableStyle style 
= new DataGridTableStyle();
          style.MappingName 
= _dataTable.TableName;
          
foreach(DataColumn column