using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text; namespace Dev.GridControl
{
public class GridMethod
{
string path = System.Windows.Forms.Application.StartupPath + "\\layout\\";
/// <summary>
/// 列名称
/// </summary>
public Dictionary<string, string> DicColName { get; set; }
/// <summary>
/// 数据源
/// </summary>
public DataTable Source { get; set; }
/// <summary>
/// 列宽
/// </summary>
public int IndicatorWidth { get; set; }
/// <summary>
/// GridContrl -- GridView
/// </summary>
public GridView GView { get; set; } public GridMethod()
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
} /// <summary>
/// 选中行 字体颜色、背景颜色设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
GridView gridView1 = (GridView)sender;
gridView1.Appearance.FocusedRow.ForeColor = Color.Red;//字体颜色
gridView1.Appearance.FocusedRow.BackColor = Color.Linen;//行背景颜色
}
/// <summary>
/// 初始化GridContrl--GridView的样式和事件
/// </summary>
/// <param name="gridView1">gridView1</param>
/// <param name="IndicatorWidth">-1 列的宽度</param>
/// <param name="dt"> 绑定的数据源</param>
/// <param name="ColCaption">列标题</param>
public void GetSetGridContrl2(DataTable dt, GridView gridView1, int IndicatorWidth, string NameSpacePath)
{
//设置列索引为-1的列宽
gridView1.IndicatorWidth = IndicatorWidth;
gridView1.OptionsView.ShowAutoFilterRow = true;
gridView1.OptionsView.ColumnAutoWidth = false;
//自动列宽,会出现横向滚动条
gridView1.OptionsView.ColumnAutoWidth = false;
//自动列宽
gridView1.BestFitColumns();
gridView1.Appearance.OddRow.BackColor = Color.PaleGoldenrod; // 设置奇数行颜色 // 默认也是白色 可以省略
gridView1.OptionsView.EnableAppearanceOddRow = true; // 使能 // 和和上面绑定 同时使用有效
gridView1.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 设置偶数行颜色
gridView1.OptionsView.EnableAppearanceEvenRow = true; // 使能 // 和和上面绑定 同时使用有效
gridView1.OptionsView.ShowGroupPanel = false;
string FileNames = "", REM = "", LONG = "", type2 = "";
try
{ for (int i = ; i < dt.Rows.Count; i++)
{
FileNames = dt.Rows[i]["FileNames"].ToString();
REM = dt.Rows[i]["REM"].ToString();
LONG = dt.Rows[i]["LONG"].ToString();
type2 = dt.Rows[i]["type2"].ToString(); DevExpress.XtraGrid.Columns.GridColumn Col1 = new DevExpress.XtraGrid.Columns.GridColumn();
try
{
Col1.Width = Convert.ToInt32(LONG);
}
catch (Exception ex)
{
Col1.Width =;
}
Col1.Tag = FileNames;
Col1.Name = "Col" + FileNames;
Col1.FieldName = FileNames;
Col1.Caption = REM; if (type2 == "时间")//时间格式
{
Col1.DisplayFormat.FormatType = FormatType.DateTime;
Col1.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; }
if (type2 == "数值")//数值格式,最大4位小数
{
Col1.DisplayFormat.FormatString = "N4";
Col1.DisplayFormat.FormatType = FormatType.Numeric;
}
Col1.VisibleIndex = i;
gridView1.Columns.Add(Col1);
}
}
catch (Exception)
{ } //添加 行索引事件
gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
//布局变化时产生事件
gridView1.Layout += gridView1_Layout;
//保存表格样式的.xml文件
string xmlfile = path + NameSpacePath.Replace(".", "") + gridView1.Name + ".xml";
gridView1.Tag = xmlfile;
LoadLayout(gridView1);//加载窗格
gridView1.OptionsView.ShowFooter = true;
}
/// <summary>
/// GridContrl--GridView 的设置
/// </summary>
/// <param name="gridView1">gridView1</param>
/// <param name="IndicatorWidth">-1 列的宽度</param>
/// <param name="dt"> 绑定的数据源</param>
/// <param name="ColCaption">列标题</param>
public void GetSetGridContrl(GridView gridView1, int IndicatorWidth, string NameSpacePath)
{
//设置列索引为-1的列宽
gridView1.IndicatorWidth = IndicatorWidth;
gridView1.OptionsView.ShowAutoFilterRow = true;
gridView1.OptionsView.ColumnAutoWidth = false;
//自动列宽,会出现横向滚动条
gridView1.OptionsView.ColumnAutoWidth = false;
//自动列宽
gridView1.BestFitColumns();
gridView1.Appearance.OddRow.BackColor = Color.PaleGoldenrod; // 设置奇数行颜色 // 默认也是白色 可以省略
gridView1.OptionsView.EnableAppearanceOddRow = true; // 使能 // 和和上面绑定 同时使用有效
gridView1.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 设置偶数行颜色
gridView1.OptionsView.EnableAppearanceEvenRow = true; // 使能 // 和和上面绑定 同时使用有效
gridView1.OptionsView.ShowGroupPanel = false;
//添加 行索引事件
gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
//布局变化时产生事件
gridView1.Layout += gridView1_Layout;
string xmlfile = path + NameSpacePath.Replace(".", "") + gridView1.Name + ".xml";
gridView1.Tag = xmlfile;
LoadLayout(gridView1);//加载窗格
gridView1.OptionsView.ShowFooter = true;
gridView1.RowStyle += gridView1_RowStyle;
} /// <summary>
/// GridContrl--GridView 的设置
/// </summary>
/// <param name="gridView1">gridView1</param>
/// <param name="IndicatorWidth">-1 列的宽度</param>
/// <param name="dt"> 绑定的数据源</param>
/// <param name="ColCaption">列标题</param>
public void GetSetGridContrl(DataTable dt, GridView gridView1, int IndicatorWidth, string NameSpacePath)
{
//设置列索引为-1的列宽
gridView1.IndicatorWidth = IndicatorWidth;
gridView1.OptionsView.ShowAutoFilterRow = true;
gridView1.OptionsView.ColumnAutoWidth = false;
//自动列宽,会出现横向滚动条
gridView1.OptionsView.ColumnAutoWidth = false;
//自动列宽
gridView1.BestFitColumns();
gridView1.Appearance.OddRow.BackColor = Color.PaleGoldenrod; // 设置奇数行颜色 // 默认也是白色 可以省略
gridView1.OptionsView.EnableAppearanceOddRow = true; // 使能 // 和和上面绑定 同时使用有效
gridView1.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 设置偶数行颜色
gridView1.OptionsView.EnableAppearanceEvenRow = true; // 使能 // 和和上面绑定 同时使用有效
gridView1.OptionsView.ShowGroupPanel = false;
string FileNames = "", REM = "", LONG = "", type2 = "";
try
{ for (int i = ; i < dt.Rows.Count; i++)
{
FileNames = dt.Rows[i]["FileNames"].ToString();
REM = dt.Rows[i]["REM"].ToString();
LONG = dt.Rows[i]["LONG"].ToString();
type2 = dt.Rows[i]["type2"].ToString(); DevExpress.XtraGrid.Columns.GridColumn Col1 = new DevExpress.XtraGrid.Columns.GridColumn();
try
{
Col1.Width = Convert.ToInt32(LONG);
}
catch (Exception ex)
{
Col1.Width =;
}
Col1.Tag = FileNames;
Col1.Name = "Col" + FileNames;
Col1.FieldName = FileNames;
Col1.Caption = REM; if (type2 == "时间")
{
Col1.DisplayFormat.FormatType = FormatType.DateTime;
Col1.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; }
if (type2 == "数值")
{
Col1.DisplayFormat.FormatString = "N4";
Col1.DisplayFormat.FormatType = FormatType.Numeric;
}
Col1.VisibleIndex = i;
gridView1.Columns.Add(Col1);
}
}
catch (Exception)
{ } //添加 行索引事件
gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
//布局变化时产生事件
gridView1.Layout += gridView1_Layout;
string xmlfile = path + NameSpacePath.Replace(".", "") + gridView1.Name + ".xml";
gridView1.Tag = xmlfile;
LoadLayout(gridView1);//加载窗格
gridView1.OptionsView.ShowFooter = true;
gridView1.RowStyle+=gridView1_RowStyle; } /// <summary>
/// 布局变化时,自动保存窗格样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void gridView1_Layout(object sender, EventArgs e)
{
GridView gridView1 = (GridView)sender;
SaveLayout(gridView1);//布局变化时 自动保存窗格布局
} /// <summary>
/// 保存风格的方法
/// </summary>
/// <param name="moduleid"></param>
/// <param name="saveType"></param>
public void SaveLayout(GridView gridView1)
{
string file = gridView1.Tag.ToString();
if (File.Exists(file))
{
File.Delete(file);
}
gridView1.SaveLayoutToXml(file);
} /// <summary>
/// 加载风格
/// </summary>
/// <param name="moduleid"></param>
/// <param name="saveType"></param>
public void LoadLayout(GridView gridView1)
{
string file = gridView1.Tag.ToString();
if (!File.Exists(file))
{
return;
}
gridView1.RestoreLayoutFromXml(file);
} /// <summary>
/// 添加行索引,过滤行筛选条件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
{
GridView gridView1 = (GridView)sender;
//设置行索引:列索引等于-1处设置行索引。
if (e.Info.IsRowIndicator && e.RowHandle >= )
{
e.Info.DisplayText = (e.RowHandle + ).ToString();
}
//设置筛选行不用输入百分号直接筛选
foreach (DevExpress.XtraGrid.Columns.GridColumn item in gridView1.Columns)
{
item.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains; //筛选条件设置为包含
} }
}
}
05-08 08:10