DataGrid 风格管理类测试版源码

2024-01-29 04:32

本文主要是介绍DataGrid 风格管理类测试版源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

众所周知,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 in _dataTable.Columns) {

            
if(column.DataType==System.Type.GetType("System.Boolean")){
              DataGridBoolColumn gridColumn 
= new DataGridBoolColumn();
              gridColumn.TrueValue 
= true;
              gridColumn.FalseValue 
= false;
              gridColumn.MappingName 
= column.ColumnName;
              gridColumn.HeaderText 
= column.ColumnName;
              gridColumn.Width 
= _dataGrid.PreferredColumnWidth;
              style.GridColumnStyles.Add(gridColumn);

            }
else{
              DataGridTextBoxColumn gridColumn 
= new DataGridTextBoxColumn();
              gridColumn.MappingName 
= column.ColumnName;
              gridColumn.HeaderText 
= column.ColumnName;
              gridColumn.Width 
= _dataGrid.PreferredColumnWidth;
              gridColumn.NullText 
= "";
              style.GridColumnStyles.Add(gridColumn);

            }

          }

          _dataGrid.TableStyles.Clear();
          _dataGrid.TableStyles.Add(style);
        }


        
return _dataGrid.TableStyles[0];
      }
catch{
        
return null;
      }

    }


    
private bool isBoolColumn(string ColumnName){
      
return (_dataTable.Columns[ColumnName].DataType==System.Type.GetType("System.Boolean"));
    }


    
private int getColumnIndex(DataGridTableStyle DGTS , string columName){
      
for(int i=0;i<DGTS.GridColumnStyles.Count;i++){
        
if(DGTS.GridColumnStyles[i].HeaderText.ToString().Trim()==columName.ToString().Trim()){
          
return i;
        }

      }

      
return -1;
    }


    
/// <summary>
    
/// 设置只读的行
    
/// </summary>
    
/// <param name="rowIndexs">行索引数组</param>
    
/// <param name="Readonly">是否只读</param>
    
/// <returns>是否成功</returns>

    public bool SetReadOnlyRows(int[] rowIndexs,bool Readonly){
      
try{
        
this.isReadonlyRow = Readonly;
        
this.ReadonlyRows = rowIndexs;
        
return true;
      }
catch{
        
return false;
      }

    }

    
/// <summary>
    
/// 设置列只读属性
    
/// </summary>
    
/// <param name="columnIndexs">列索引数组</param>
    
/// <param name="Readonly">只读属性</param>
    
/// <returns>是否处理成功</returns>

    public bool SetReadOnlyColumns(int[] columnIndexs,bool Readonly){
      
//获取表风格
      try{
        DataGridTableStyle currGTS 
= GetGridTableStyle();
        
if(currGTS==null){
          
throw new Exception("初始化表风格出错!");
        }


        
if(columnIndexs==null||columnIndexs.Length<=0){
          
throw new ArgumentOutOfRangeException("只读索引超出范围!");
        }


        DataGridColumnStyle GCS;

        
for(int i=0;i<columnIndexs.Length;i++){
      
          
if(columnIndexs[i]<0&&columnIndexs[i]>currGTS.GridColumnStyles.Count-1){
            
throw new ArgumentOutOfRangeException("索引超出范围!");
          }


          GCS 
= currGTS.GridColumnStyles[columnIndexs[i]];
          
if(GCS is DataGridBoolColumn){
            (GCS 
as DataGridBoolColumn).ReadOnly = Readonly;
          }
else if(GCS is DataGridTextBoxColumn){
            (GCS 
as DataGridTextBoxColumn).ReadOnly = Readonly;
          }

        }

        
return true;
      }
catch{return false;}
    }


    
/// <summary>
    
/// 设置列只读属性
    
/// </summary>
    
/// <param name="columnNames">列名称数组</param>
    
/// <param name="Readonly">只读属性</param>
    
/// <returns>是否处理成功</returns>

    public bool SetReadOnlyColumns(string[] columnNames,bool Readonly){
      
//获取表风格
      try{
        DataGridTableStyle currGTS 
= GetGridTableStyle();
        
if(currGTS==null){
          
throw new Exception("初始化表风格出错!");
        }


        
if(columnNames==null||columnNames.Length<=0){
          
throw new ArgumentOutOfRangeException("只读索引超出范围!");
        }


        DataGridColumnStyle GCS;
        
int columnIndex =-1;
        
for(int i=0;i<columnNames.Length;i++){
          columnIndex 
= getColumnIndex(currGTS,columnNames[i]);
          
if(columnIndex==-1){
            
throw new Exception("列名称为空或者无效!");
          }

          GCS 
= currGTS.GridColumnStyles[columnNames[i]];
          
if(GCS is DataGridBoolColumn){
            (GCS 
as DataGridBoolColumn).ReadOnly = Readonly;
          }
else if(GCS is DataGridTextBoxColumn){
            (GCS 
as DataGridTextBoxColumn).ReadOnly = Readonly;
          }

        }

        
return true;
      }
catch{return false;}
    }


    
/// <summary>
    
/// 设置列属性
    
/// </summary>
    
/// <param name="columnIndex">列索引值</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <param name="Format">列格式</param>
    
/// <param name="Align">靠齐方式</param>
    
/// <param name="Visible">是否可见</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format,HorizontalAlignment Align,bool Visible){
      
try{
        
//获取表风格
        DataGridTableStyle currGTS = GetGridTableStyle();
        
string MappingName= null;
        
if(currGTS==null){
          
throw new Exception("初始化表风格出错!");
        }


        
if(columnIndex<0&&columnIndex>currGTS.GridColumnStyles.Count-1){
          
throw new ArgumentOutOfRangeException("索引超出范围!");
        }


        
//处理参数
        Width   = (Math.Abs(Width));
        Title   
= (Title   == null ? "" : Title);
        Format  
= ((object)Format  == null ? FormatIndex.Text : Format);
        Align   
= ((object)Align   == null ? HorizontalAlignment.Left : Align);

        MappingName 
= currGTS.GridColumnStyles[columnIndex].MappingName;
        
        DataGridColumnStyle GCS 
= currGTS.GridColumnStyles[columnIndex];

        
if(GCS is DataGridTextBoxColumn){
          DataGridTextBoxColumn gridColumn 
= (GCS as DataGridTextBoxColumn);
          
switch((int)Format){
            
case 2
              gridColumn.Format 
= "C";
              
break;
            
case 3:
              gridColumn.Format 
= "N";
              
break;
            
case 5:
              gridColumn.Format
=System.String.Format("yyyy年MM月dd日 hh:mm:ss",gridColumn.TextBox);
              
break;
            
case 6:
              gridColumn.Format
=System.String.Format("yyyy-MM-dd hh:mm:ss",gridColumn.TextBox);
              
break;
            
case 7:
              gridColumn.Format
=System.String.Format("yyyy-MM-dd",gridColumn.TextBox);
              
break;
            
case 8:
              gridColumn.Format
=System.String.Format("yy-MM-dd",gridColumn.TextBox);
              
break;
            
case 9:
              gridColumn.Format
= System.String.Format("hh:mm:ss",gridColumn.TextBox);
              
break;
            
default:
              gridColumn.Format 
= "";
              
break;
          }


          gridColumn.Width 
= Width;
          gridColumn.MappingName 
= MappingName;
          gridColumn.HeaderText 
= (Title.ToString()==""||Title==null ? MappingName : Title.ToString());
          gridColumn.Alignment 
= Align;
          gridColumn.NullText 
= "";
        }


        
//隐藏字段
        if(!Visible){
          GCS.Width 
=0;
          GCS.HeaderText 
="";
        }


        
return true;
      }
catch{
        
return false;
      }

    }

    
/// <summary>
    
/// 设置列属性,默认可见
    
/// </summary>
    
/// <param name="columnIndex">列索引值</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <param name="Format">列格式</param>
    
/// <param name="Align">靠齐方式</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format,HorizontalAlignment Align){
      
return SetColumn(columnIndex,Width,Title,Format,Align,true);
    }

    
/// <summary>
    
/// 设置列属性,默认可见,默认靠齐居左
    
/// </summary>
    
/// <param name="columnIndex">列索引值</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <param name="Format">列格式</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format){
      
return SetColumn(columnIndex,Width,Title,Format,HorizontalAlignment.Left,true);
    }

    
/// <summary>
    
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式
    
/// </summary>
    
/// <param name="columnIndex">列索引值</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(int columnIndex,int Width,string Title){
      
return SetColumn(columnIndex,Width,Title,FormatIndex.Text,HorizontalAlignment.Left,true);
    }

    
/// <summary>
    
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式,默认标题为影射名
    
/// </summary>
    
/// <param name="columnIndex">列索引值</param>
    
/// <param name="Width">宽度</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(int columnIndex,int Width){
      
return SetColumn(columnIndex,Width,"",FormatIndex.Text,HorizontalAlignment.Left,true);
    }


    
/// <summary>
    
/// 设置列属性
    
/// </summary>
    
/// <param name="columnName">列名称</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <param name="Format">列格式</param>
    
/// <param name="Align">靠齐方式</param>
    
/// <param name="Visible">是否可见</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(string columnName,int Width,string Title,FormatIndex Format,HorizontalAlignment Align,bool Visible){
      DataGridTableStyle currGTS 
= GetGridTableStyle();
      
if(currGTS==null){
        
throw new Exception("初始化表风格出错!");
      }

      
int columnIndex = getColumnIndex(currGTS,columnName);
      
if(columnIndex==-1){
        
throw new Exception("列名称为空或者无效!");
      }


      
return SetColumn(columnIndex,Width,Title,Format,Align,Visible);      
    }

    
/// <summary>
    
/// 设置列属性,默认可见
    
/// </summary>
    
/// <param name="columnName">列名称</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <param name="Format">列格式</param>
    
/// <param name="Align">靠齐方式</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(string columnName,int Width,string Title,FormatIndex Format,HorizontalAlignment Align){
      DataGridTableStyle currGTS 
= GetGridTableStyle();
      
if(currGTS==null){
        
throw new Exception("初始化表风格出错!");
      }

      
int columnIndex = getColumnIndex(currGTS,columnName);
      
if(columnIndex==-1){
        
throw new Exception("列名称为空或者无效!");
      }


      
return SetColumn(columnIndex,Width,Title,Format,Align);      
    }

    
/// <summary>
    
/// 设置列属性,默认可见,默认靠齐居左
    
/// </summary>
    
/// <param name="columnName">列名称</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <param name="Format">列格式</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(string columnName,int Width,string Title,FormatIndex Format){
      DataGridTableStyle currGTS 
= GetGridTableStyle();
      
if(currGTS==null){
        
throw new Exception("初始化表风格出错!");
      }

      
int columnIndex = getColumnIndex(currGTS,columnName);
      
if(columnIndex==-1){
        
throw new Exception("列名称为空或者无效!");
      }


      
return SetColumn(columnIndex,Width,Title,Format);      
    }

    
/// <summary>
    
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式
    
/// </summary>
    
/// <param name="columnName">列名称</param>
    
/// <param name="Width">宽度</param>
    
/// <param name="Title">标题文本</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(string columnName,int Width,string Title){
      DataGridTableStyle currGTS 
= GetGridTableStyle();
      
if(currGTS==null){
        
throw new Exception("初始化表风格出错!");
      }

      
int columnIndex = getColumnIndex(currGTS,columnName);
      
if(columnIndex==-1){
        
throw new Exception("列名称为空或者无效!");
      }


      
return SetColumn(columnIndex,Width,Title);      
    }

    
/// <summary>
    
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式,默认标题为影射名
    
/// </summary>
    
/// <param name="columnName">列名称</param>
    
/// <param name="Width">宽度</param>
    
/// <returns>完成状态</returns>

    public bool SetColumn(string columnName,int Width){
      DataGridTableStyle currGTS 
= GetGridTableStyle();
      
if(currGTS==null){
        
throw new Exception("初始化表风格出错!");
      }

      
int columnIndex = getColumnIndex(currGTS,columnName);
      
if(columnIndex==-1){
        
throw new Exception("列名称为空或者无效!");
      }


      
return SetColumn(columnIndex,Width);      
    }

  }

}

下面的代码是演示如何使用这个类:

      OleDbConnection conn  =   new  OleDbConnection(  " Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=/ " E://sf//DataBasew.mdb/ " ;Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=/ " Microsoft.Jet.OLEDB. 4.0 / " ;Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1 "  );
            
      OleDbDataAdapter da 
=   new  OleDbDataAdapter( " SELECT A.设备编号, b.设备名称, b.所属设备组, A.检测时间, A.气体压力, A.换算压力, A.气体湿度, A.气体温度, A.诊断代码 FR "   +
        
" OM (六氟化硫数据 A LEFT OUTER JOIN 测试设备 b ON A.设备编号 = b.编号) ORDER BY b.所属设备组, A.检测时间 " , conn );
      DataSet ds 
=   new  DataSet();
      
try   {
        da.Fill( ds, 
"六氟化硫数据" );
      }

      
catch ( Exception ex )  {
        MessageBox.Show( ex.Message );
      }


      
// 这里不需要制定,因为要由风格管理类接管,并且避免dataGrid自动初始化表风格影响速度
      
// dataGrid.DataSource = ds.Tables[0];

      Grid.Service.StyleManager sm 
=   new  Grid.Service.StyleManager();
      sm.DataGrid 
=  dataGrid;   // 目标DataGrid
      sm.DataSource  =  ds.Tables[ 0 ];   // 目标DataTable
      sm.AllowSelectedRow  =   true // 允许行选择
      
// 设置第1列宽200,名称为“设备编号!",数据类型为数值型,局中,不隐藏
      sm.SetColumn( 0 , 200 , " 设备编号 ! " ,Grid.Service.FormatIndex.Numeric,HorizontalAlignment.Center, true );
      
// 设置第2列宽100,不制定名称“用表字段真实名称”,文本类型
      sm.SetColumn( 1 , 100 ,( string ) null ,Grid.Service.FormatIndex.Text,HorizontalAlignment.Center, true );
      
// 设置第4列为日期时间类型
      sm.SetColumn( 3 , 200 ,( string ) null ,Grid.Service.FormatIndex.DateTime);
      sm.SetReadOnlyColumns(
new   int [] {0,2,5} , true );  // 只读列0,2,5
      sm.SetReadOnlyRows( new   int [] {0,4,6,8} , true ); // 只读行0,4,6,8
      sm.SetHeader( new  Font( " 黑体 " ,9f),Color.Red);  // 设置列头字体,黑体9号字,红色
      sm.SetGrid( " 测试表格 " , true , false );  // 显示表格标题,不显示行标题
      
      conn.Close();

这篇关于DataGrid 风格管理类测试版源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/655731

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

nvm如何切换与管理node版本

《nvm如何切换与管理node版本》:本文主要介绍nvm如何切换与管理node版本问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录nvm切换与管理node版本nvm安装nvm常用命令总结nvm切换与管理node版本nvm适用于多项目同时开发,然后项目适配no

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

Redis实现RBAC权限管理

《Redis实现RBAC权限管理》本文主要介绍了Redis实现RBAC权限管理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1. 什么是 RBAC?2. 为什么使用 Redis 实现 RBAC?3. 设计 RBAC 数据结构

mac安装nvm(node.js)多版本管理实践步骤

《mac安装nvm(node.js)多版本管理实践步骤》:本文主要介绍mac安装nvm(node.js)多版本管理的相关资料,NVM是一个用于管理多个Node.js版本的命令行工具,它允许开发者在... 目录NVM功能简介MAC安装实践一、下载nvm二、安装nvm三、安装node.js总结NVM功能简介N

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用