最近学习DevExpress的控件,写了几个方法拿来分享,希望大家能用的上:
using System;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DevExpress;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraGrid.Views.BandedGrid;
namespace MyDev
{
public class DevGridView
{
#region Members
private DevExpress.XtraGrid.GridControl gridControl;
#endregion
#region Construct
/// <summary>
///
/// </summary>
public DevGridView()
{ }
#endregion
#region Perporties
public GridControl GridControl
{
get { return gridControl; }
set { gridControl = value; }
}
#endregion
#region Methods
#region 初始化Dev GridView 表头
/// <summary>
/// 初始化Dev GridView 表头
/// </summary>
/// <param name="gridView"></param>
/// <param name="gridColumn"></param>
public static void InitGridViewTitle(GridView gridView, GridColumn[] gridColumn)
{
gridView.Columns.AddRange(gridColumn);
}
/// <summary>
/// 初始化Dev GridView 表头
/// </summary>
/// <param name="gridView"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
public static void InitGridViewTitle(GridView gridView, string[] sCaption, string[] sFieldName, int[] iWidth)
{
DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
for (int i = 0; i < sCaption.Length; i++)
{
col = new DevExpress.XtraGrid.Columns.GridColumn();
col.Caption = sCaption[i];
col.FieldName = sFieldName[i];
col.Name = sFieldName[i];
col.Width = iWidth[i];
col.VisibleIndex = i;
gridView.Columns.Add(col);
}
}
/// <summary>
/// 初始化Dev GridView 表头
/// </summary>
/// <param name="gridView"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
public static void InitGridViewTitle(GridControl gridControl, GridView gridView, string[] sCaption, string[] sFieldName, int[] iWidth)
{
DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
for (int i = 0; i < sCaption.Length; i++)
{
col = new DevExpress.XtraGrid.Columns.GridColumn();
col.Caption = sCaption[i];
col.FieldName = sFieldName[i];
col.Width = iWidth[i];
col.VisibleIndex = i;
gridView.Columns.Add(col);
}
gridView.GridControl = gridControl;
gridControl.ViewCollection.Add(gridView);
}
#endregion // end InitGridView
#region 初始化Dev CardView 表头
/// <summary>
/// 初始化Dev CardView 表头
/// </summary>
/// <param name="cardView"></param>
/// <param name="gridColumn"></param>
public static void InitCardViewTitle(CardView cardView,GridColumn[] gridColumn)
{
cardView.Columns.AddRange(gridColumn);
}
/// <summary>
/// 初始化Dev CardView 表头
/// </summary>
/// <param name="cardView"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
public static void InitCardViewTitle(CardView cardView, string[] sCaption, string[] sFieldName, int[] iWidth)
{
DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
for (int i = 0; i < sCaption.Length; i++)
{
col = new DevExpress.XtraGrid.Columns.GridColumn();
col.Caption = sCaption[i];
col.FieldName = sFieldName[i];
col.Name = sFieldName[i];
col.Width = iWidth[i];
col.VisibleIndex = i;
cardView.Columns.Add(col);
}
}
#endregion
#region InitGridColumn 初始化列
/// <summary>
/// 初始化一列
/// </summary>
/// <param name="gridColumn"></param>
/// <param name="sName"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
/// <param name="iVisibleIndex"></param>
public GridColumn InitGridColumn( string sName, string sCaption, string sFieldName, int iWidth, int iVisibleIndex)
{
GridColumn gridColumn = new GridColumn();
gridColumn.Name = sName;
gridColumn.Caption = sCaption;
gridColumn.FieldName = sFieldName;
gridColumn.Width = iWidth;
gridColumn.VisibleIndex = iVisibleIndex;
return gridColumn;
}
/// <summary>
/// 初始化多列,要求给出参数的维数相同
/// </summary>
/// <param name="gridColumn"></param>
/// <param name="sName"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
/// <param name="iVisibleIndex"></param>
public GridColumn[] InitGridColumn(string[] sName, string[] sCaption, string[] sFieldName, int[] iWidth, int[] iVisibleIndex)
{
int iLen = sCaption.Length;
GridColumn[] gridColumn=new GridColumn[iLen];
for (int i = 0; i < gridColumn.Length; i++)
{
gridColumn[i].Name = sName[i];
gridColumn[i].Caption = sCaption[i];
gridColumn[i].FieldName = sFieldName[i];
gridColumn[i].Width = iWidth[i];
gridColumn[i].VisibleIndex = iVisibleIndex[i];
}
return gridColumn;
}
#endregion // end InitGridColumn
#region InitGridBand 初始化多表头的GridBand
/// <summary>
/// 初始化多表头的GridBand
/// </summary>
/// <param name="gridBand">大表头</param>
/// <param name="sName"></param>
/// <param name="sCaption"></param>
/// <param name="iWidth"></param>
/// <param name="gridColumn">小表头</param>
public void InitGridBand(DevExpress.XtraGrid.Views.BandedGrid.GridBand gridBand,string sName,string sCaption,int iWidth,
DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] BandedGridColumn)
{
gridBand.Name = sName;
gridBand.Caption = sCaption;
gridBand.Width = iWidth;
for (int i = 0; i < BandedGridColumn.Length; i++)
{
gridBand.Columns.Add(BandedGridColumn[i]);
}
}
/// <summary>
///
/// </summary>
/// <param name="sName"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
/// <param name="iVisibleIndex"></param>
/// <returns></returns>
public BandedGridColumn InitBandedGridColumn(string sName,string sCaption,string sFieldName,int iWidth,int iVisibleIndex)
{
DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn bandedGridColumn = new BandedGridColumn();
bandedGridColumn.Name = sName;
bandedGridColumn.Caption = sCaption;
bandedGridColumn.FieldName = sFieldName;
bandedGridColumn.Width = iWidth;
bandedGridColumn.VisibleIndex = iVisibleIndex;
return bandedGridColumn;
}
/// <summary>
///
/// </summary>
/// <param name="sName"></param>
/// <param name="sCaption"></param>
/// <param name="sFieldName"></param>
/// <param name="iWidth"></param>
/// <param name="iVisibleIndex"></param>
/// <returns></returns>
public BandedGridColumn[] InitBandedGridColumn(string[] sName, string[] sCaption, string[] sFieldName, int[] iWidth, int[] iVisibleIndex)
{
int iLen= sName.Length;
DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] bandedGridColumn=new BandedGridColumn[iLen];
for (int i = 0; i < bandedGridColumn.Length; i++)
{
bandedGridColumn[i].Name = sName[i];
bandedGridColumn[i].Caption = sCaption[i];
bandedGridColumn[i].FieldName = sFieldName[i];
bandedGridColumn[i].Width = iWidth[i];
bandedGridColumn[i].VisibleIndex = iVisibleIndex[i];
}
return bandedGridColumn;
}
#endregion
#region ChangGridColumnToBandedGridColumn
/// <summary>
///
/// </summary>
/// <param name="gridColumn"></param>
/// <returns></returns>
public BandedGridColumn ChangGridColumnTo(GridColumn gridColumn)
{
return (BandedGridColumn)gridColumn;
}
/// <summary>
///
/// </summary>
/// <param name="gridColumn"></param>
/// <returns></returns>
public BandedGridColumn[] ChangGridColumnTo(GridColumn[] gridColumn)
{
return (BandedGridColumn[])gridColumn;
}
/// <summary>
///
/// </summary>
/// <param name="bandedGridColumn"></param>
/// <returns></returns>
public GridColumn ChangBandedGridColumnTo(BandedGridColumn bandedGridColumn)
{
return (GridColumn)bandedGridColumn;
}
/// <summary>
///
/// </summary>
/// <param name="bandedGridColumn"></param>
/// <returns></returns>
public GridColumn[] ChangBandedGridColumnTo(BandedGridColumn[] bandedGridColumn)
{
return (GridColumn[])bandedGridColumn;
}
#endregion
#region SetGridControlDataSource 添加数据源
/// <summary>
///
/// </summary>
/// <param name="gridControl"></param>
/// <param name="dt"></param>
public void SetGridControlDataSource(DevExpress.XtraGrid.GridControl gridControl,DataTable dt)
{
gridControl.DataSource = dt;
}
/// <summary>
///
/// </summary>
/// <param name="gridControl"></param>
/// <param name="ds"></param>
public void SetGridControlDataSource(DevExpress.XtraGrid.GridControl gridControl, DataSet ds)
{
gridControl.DataSource = ds.Tables[0];
}
/// <summary>
///
/// </summary>
/// <param name="gridControl"></param>
/// <param name="ds"></param>
/// <param name="sTableName"></param>
public void SetGridControlDataSource(DevExpress.XtraGrid.GridControl gridControl, DataSet ds,string sTableName)
{
gridControl.DataSource = ds.Tables[sTableName];
}
#endregion // end SetGridControlDataSource
#region ExportGridViewDataSource 导出GridView数据源
/// <summary>
/// 返回 DataView
/// </summary>
/// <param name="gridView"></param>
/// <returns></returns>
public DataView ExportGridViewDataSource(DevExpress.XtraGrid.Views.Grid.GridView gridView)
{
return (DataView)gridView.DataSource;
}
/// <summary>
/// 返回 Datable
/// </summary>
/// <param name="gridView"></param>
/// <param name="sTableName"></param>
/// <returns></returns>
public DataTable ExportGridViewDataSource(GridView gridView, string sTableName)
{
DataView dv = (DataView)gridView.DataSource;
return dv.ToTable(sTableName);
}
#endregion
#endregion // end Methods
}
}