WinForm中使用XtraGrid控件,实现在界面中动态修改列显示,列名列宽等

在使用XtraGrid的gridControl或DataGridView中,里面栏目的设置比较麻烦。为此我找出了一个比较简便的解决方法。

大致思路如下:定义一个和表结构类似的XML文件,保存表字段的显示标题、是否显示、宽度等信息,在GridControl显示的时候,直接读取这些信息。再在工具条中加上栏目设置的功能。

具体实现步骤:

1. 当然使用传说中的CodeSmith生成XML文件,CodeSmith模板代码如下:
<%@ CodeTemplate Language="C#" TargetLanguage="text" ResponseEncoding="UTF-8" Description="Generates a update stored procedure." %>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Database that the documentation should be based on." %>
<%@ Property Name="Author" Type="System.String" Default="Pantao" Optional="False" Category="Description" Description="About Author" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<script runat="template">
public string GetSqlParameterStatement(ColumnSchema column)
{
string param = "";

switch (column.DataType)
{
case DbType.Decimal:
{
param += "(" + column.Precision + ", " + column.Scale + ")";
break;
}
case DbType.Boolean:
case DbType.Int32:
case DbType.DateTime:
case DbType.Double:
case DbType.Single:
case DbType.Byte:
case DbType.Int64:
case DbType.Int16:

break;
default:
{
if (column.Size > 0)
{
param += "(" + column.Size + ")";
}
break;
}
}

return param;
}
</script>
<% TableSchemaCollection tables = new TableSchemaCollection(SourceDatabase.Tables); %>
DataBase(Tables:<% =tables.Count.ToString() %>)
Author:<% =Author %> Date:<% =DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() %>
<% for(int i=0; i<tables.Count; i++) { %>

<% =i + 1 + "." %>TableName:【<% =tables[i].Name %>】
<% for(int k=0; k<tables[i].Columns.Count; k++) { %>
(<% = k + 1 %>). <% =tables[i].Columns[k].Name %> <% if (tables[i].Columns[k].IsPrimaryKeyMember) {%>Primary<% } %> <% =tables[i].Columns[k].NativeType + GetSqlParameterStatement(tables[i].Columns[k]) %> <% if(tables[i].Columns[k].AllowDBNull) { %>AllowNull<% } %>
<% ="[Description:]" + tables[i].Columns[k].Description %>
<% } %>
<% } %>
生成的XML文件格式如下:
<?xml version="1.0" standalone="yes"?>
<xml>
<GridView>
<Caption>編號</Caption>
<FieldName>ID</FieldName>
<ColumnName>gridColumn1</ColumnName>
<Visible>false</Visible>
<VisibleIndex>-1</VisibleIndex>
<Fixed>None</Fixed>
<Width>20</Width>
<ColumnEdit />
</GridView>
<GridView>
<Caption>紗線名稱</Caption>
<FieldName>YarnName</FieldName>
<ColumnName>gridColumn2</ColumnName>
<Visible>true</Visible>
<VisibleIndex>1</VisibleIndex>
<Fixed>Left</Fixed>
<Width>200</Width>
<ColumnEdit />
</GridView>
....................................
</xml>

读取xml文件,转换为gridControl中Column格式的方法:
public static void SetGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
{
gridView.Columns.Clear();

DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
gridCoulumn.Caption = ds.Tables[0].Rows[i]["Caption"].ToString();
gridCoulumn.FieldName = ds.Tables[0].Rows[i]["FieldName"].ToString();
gridCoulumn.Name = ds.Tables[0].Rows[i]["ColumnName"].ToString();
gridCoulumn.VisibleIndex = int.Parse(ds.Tables[0].Rows[i]["VisibleIndex"].ToString());
gridCoulumn.Visible = ds.Tables[0].Rows[i]["Visible"].ToString().ToLower() == "true" ? true : false;
if (!gridCoulumn.Visible)
{
gridCoulumn.VisibleIndex = -1;
}
gridCoulumn.Width = int.Parse(ds.Tables[0].Rows[i]["Width"].ToString());
switch (ds.Tables[0].Rows[i]["Fixed"].ToString().ToLower())
{
case "none":
gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
break;
case "left":
gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
break;
case "right":
gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
break;
}
if (ds.Tables[0].Rows[i]["ColumnEdit"].ToString() == "RepositoryItemLookUpEdit")
{
}

gridView.Columns.Add(gridCoulumn);
}
}
================
在窗体绑定数据之前,设定窗体的栏目:YarnNew.Classes.Common.SetGridView(gridView1, xmlFile);

栏目设置的部分代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace YarnNew
{
public partial class FrmColumnSetup : YarnNew.FrmBase
{
public FrmColumnSetup()
{
InitializeComponent();
FixShow();
}

public delegate void RefreshEventHandler();
public event RefreshEventHandler RefreshData;

DataSet ds = new DataSet();
private string xmlFile;
public string XmlFile
{
set { xmlFile = value; }
get { return xmlFile; }
}

private void FixShow()
{
DataTable dt = new DataTable();
dt.Columns.Add("FixShow", typeof(string));
dt.Columns.Add("FixName", typeof(string));

DataRow dr1 = dt.NewRow();
dr1[0] = "无设置";
dr1[1] = "None";
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2[0] = "固定左边";
dr2[1] = "Left";
dt.Rows.Add(dr2);

DataRow dr3 = dt.NewRow();
dr3[0] = "固定右边";
dr3[1] = "Right";
dt.Rows.Add(dr3);
repositoryItemLookUpEdit1.DataSource = dt;
}

private void FrmColumnSetup_Load(object sender, EventArgs e)
{
ds = new DataSet();
ds.ReadXml(xmlFile);

DataTable dt = ds.Tables[0].Copy();
dt.Columns.Add("BoolVisible", typeof(bool));
//dt.Columns.Add("IntVisibleIndex", typeof(int));
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["BoolVisible"] = dt.Rows[i]["Visible"].ToString().ToLower() == "true" ? true : false;
//dt.Rows[i]["IntVisibleIndex"] = int.Parse(dt.Rows[i]["VisibleIndex"].ToString());
}

gridControl1.DataSource = dt;
}

private void sbSave_Click(object sender, EventArgs e)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ds.Tables[0].Rows[i]["Caption"] = gridView1.GetRowCellDisplayText(i, gridView1.Columns["Caption"]);
ds.Tables[0].Rows[i]["Visible"] = gridView1.GetRowCellValue(i, gridView1.Columns["BoolVisible"]).ToString().ToLower();
ds.Tables[0].Rows[i]["VisibleIndex"] = gridView1.GetRowCellValue(i, gridView1.Columns["VisibleIndex"]).ToString();
ds.Tables[0].Rows[i]["Fixed"] = gridView1.GetRowCellValue(i, gridView1.Columns["Fixed"]).ToString();
ds.Tables[0].Rows[i]["Width"] = gridView1.GetRowCellValue(i, gridView1.Columns["Width"]).ToString();
}
ds.WriteXml(xmlFile);

if (RefreshData != null)
{
RefreshData();
}
this.Close();
}
}
}

你可能感兴趣的:(数据结构,xml,windows,WinForm)