由于本人是搞winform的,一直羡慕webform下能够通过绑定一个枚举或是一个数组就可以得到想用的网格数据,在winform下却要使用查询语句先将需要的数据查出后再绑定到网络控件(而且字典表还要存在于数据库)
个人一直采用这种办法在处理相关数据的显示,可能有人会有不同意见,不过在这里只是为了相说说DevExpress中XtraGrid的非绑定数据列。这个类型的列在DEMO中早就看过但一直未进行使用,后来发现它还有可以读取字典表的功能,写个笔记与大家分享。有可能是晚间新闻了,勿怪,代码非常简单,大家看下就行,至于代码中还有一个非绑定列进行合计功能也是非常有用的,特别是在dataset中有需要进行合计的项目,同时这个数据集又要回传,那么这个合计就不能写在dataset的字段中,否则当合计被更新后整个数据集中行的rowstate全部都是modify(当多用户使用时会发生两个问题,一个是会覆盖别人的数据,二是会出现并发冲突(DBConCurrencyException),这可是个人血的教训!!),而使用非绑定列则不会影响数据集中行的状态,在有的时候非常适合。
代码
using System;
using System.Collections.Generic;
using System.Data;
namespace WindowsFormsApplication1
{
public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{
public Dictionary<string, Items> items = new Dictionary<string, Items>();
public XtraForm1()
{
InitializeComponent();
}
private void AddColumn(DataTable data, string name, System.Type type) { AddColumn(data, name, type, false); }
private void AddColumn(DataTable data, string name, System.Type type, bool ro)
{
DataColumn col;
col = new DataColumn(name, type);
col.Caption = name;
col.ReadOnly = ro;
data.Columns.Add(col);
}
private void XtraForm1_Load(object sender, EventArgs e)
{
FillData();
}
private DataTable data;
private void FillData()
{
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "项目编码",
FieldName = "ItemCode",
VisibleIndex=1
}
);
//利用非绑定列读取字典表功能
//读取字典表事件见gridView1_CustomUnboundColumnData
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "项目名称",
FieldName = "ItemName",
UnboundType =DevExpress.Data.UnboundColumnType.String,
VisibleIndex = 2
}
);
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "科目1",
FieldName = "FEE1",
VisibleIndex = 3
}
);
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "科目2",
FieldName = "FEE2",
VisibleIndex =4
}
);
//利用非绑定列实现合计功能
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "合计",
FieldName = "Total",
UnboundType = DevExpress.Data.UnboundColumnType.Integer,
UnboundExpression = "FEE1+FEE2",
VisibleIndex = 5,
}
);
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "备注",
FieldName = "MEMO",
VisibleIndex = 6
}
);
data = new DataTable("ColumnsTable");
data.BeginInit();
AddColumn(data, "ItemCode", System.Type.GetType("System.String"), true);
AddColumn(data, "FEE1", System.Type.GetType("System.Double"));
AddColumn(data, "FEE2", System.Type.GetType("System.Double"));
AddColumn(data, "MEMO", System.Type.GetType("System.String"));
data.EndInit();
data.Clear();
data.Rows.Add(new object[] { "101100001", 30,30,"无" });
data.Rows.Add(new object[] { "101100002", 30,20, "无" });
data.Rows.Add(new object[] { "101100003", 20,40, "无" });
items.Add("101100001", new Items() { ItemCode = "101100001", ItemName = "收入" });
items.Add("101100002", new Items() { ItemCode = "101100002", ItemName = "支出" });
items.Add("101100003", new Items() { ItemCode = "101100003", ItemName = "其他" });
gridControl1.DataSource = data;
}
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "ItemName")
{
e.Value = items[gridView1.GetRowCellValue(e.RowHandle, "ItemCode").ToString()].ItemName;
}
}
}
public class Items
{
public string ItemCode { get; set; }
public string ItemName { get; set; }
}
}
using System.Collections.Generic;
using System.Data;
namespace WindowsFormsApplication1
{
public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{
public Dictionary<string, Items> items = new Dictionary<string, Items>();
public XtraForm1()
{
InitializeComponent();
}
private void AddColumn(DataTable data, string name, System.Type type) { AddColumn(data, name, type, false); }
private void AddColumn(DataTable data, string name, System.Type type, bool ro)
{
DataColumn col;
col = new DataColumn(name, type);
col.Caption = name;
col.ReadOnly = ro;
data.Columns.Add(col);
}
private void XtraForm1_Load(object sender, EventArgs e)
{
FillData();
}
private DataTable data;
private void FillData()
{
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "项目编码",
FieldName = "ItemCode",
VisibleIndex=1
}
);
//利用非绑定列读取字典表功能
//读取字典表事件见gridView1_CustomUnboundColumnData
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "项目名称",
FieldName = "ItemName",
UnboundType =DevExpress.Data.UnboundColumnType.String,
VisibleIndex = 2
}
);
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "科目1",
FieldName = "FEE1",
VisibleIndex = 3
}
);
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "科目2",
FieldName = "FEE2",
VisibleIndex =4
}
);
//利用非绑定列实现合计功能
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "合计",
FieldName = "Total",
UnboundType = DevExpress.Data.UnboundColumnType.Integer,
UnboundExpression = "FEE1+FEE2",
VisibleIndex = 5,
}
);
gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn()
{
Caption = "备注",
FieldName = "MEMO",
VisibleIndex = 6
}
);
data = new DataTable("ColumnsTable");
data.BeginInit();
AddColumn(data, "ItemCode", System.Type.GetType("System.String"), true);
AddColumn(data, "FEE1", System.Type.GetType("System.Double"));
AddColumn(data, "FEE2", System.Type.GetType("System.Double"));
AddColumn(data, "MEMO", System.Type.GetType("System.String"));
data.EndInit();
data.Clear();
data.Rows.Add(new object[] { "101100001", 30,30,"无" });
data.Rows.Add(new object[] { "101100002", 30,20, "无" });
data.Rows.Add(new object[] { "101100003", 20,40, "无" });
items.Add("101100001", new Items() { ItemCode = "101100001", ItemName = "收入" });
items.Add("101100002", new Items() { ItemCode = "101100002", ItemName = "支出" });
items.Add("101100003", new Items() { ItemCode = "101100003", ItemName = "其他" });
gridControl1.DataSource = data;
}
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "ItemName")
{
e.Value = items[gridView1.GetRowCellValue(e.RowHandle, "ItemCode").ToString()].ItemName;
}
}
}
public class Items
{
public string ItemCode { get; set; }
public string ItemName { get; set; }
}
}