彩票走势图

如何在DevExpress XtraGrid中显示非数据源的数据

转帖|其它|编辑:郝浩|2012-10-11 10:09:00.000|阅读 655 次

概述:介绍如何在DevExpress XtraGrid中显示非数据源的数据的两种方法。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

可以在GrideView上增加数据源没有的列,如合计、日期、序号或其他数据源等。

今天将会为大家介绍两种方法:

方法一:实现CustomColumnDisplayText事件(只能用于显示)

以下例子在bandedGridColumn1上显示 FirstName+LastName

bandedGridColumn1.OptionsColumn.AllowEdit = false;

private void bandedGridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)

{

if(e.Column.Equals(bandedGridColumn1))

{
DataRow row = bandedGridView1.GetDataRow(e.RowHandle);

e.DisplayText = string.Format("{0} {1}", row["FirstName"], row["LastName"]);

}

}

方法二: 设定列的UnboundType,并实现CustomUnboundColumnData事件(可修改值)

以下例子演示DateTime/Int/String 绑定到数据列上显示。当修改 GrideView上的值时,将修改同步到原数组中。

 

bandedGridColumn4.UnboundType = DevExpress.Data.UnboundColumnType.DateTime;

bandedGridColumn5.UnboundType = DevExpress.Data.UnboundColumnType.Integer;

bandedGridColumn6.UnboundType = DevExpress.Data.UnboundColumnType.String;


private void bandedGridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {

if(array == null) return;

if(e.ListSourceRowIndex >= array.Count) return;

Record rec = array[e.ListSourceRowIndex] as Record;

if(rec == null) return;

switch(e.Column.UnboundType) {

case UnboundColumnType.DateTime:

if(e.IsGetData)


e.Value = rec.Date;

else rec.Date = (DateTime)e.Value;

break;

case UnboundColumnType.Integer:

if(e.IsGetData)

e.Value = rec.Count;

else rec.Count = (Int32)e.Value;

break;

case UnboundColumnType.String:

if(e.IsGetData)

e.Value = rec.Comment;

else rec.Comment = e.Value.ToString();

break;

}

}


-------------------------------------------------------------

提交当前行的修改
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using System.Data.Common;
//...
public void UpdateDatasource(GridControl grid) {
//Save the latest changes to the bound DataTable
ColumnView view = (ColumnView)grid.FocusedView;
view.CloseEditor();
if(!view.UpdateCurrentRow()) return;

//Update the database's Suppliers table to which oleDBDataAdapter1 is connected
DoUpdate(oleDbDataAdapter1, dataSet11.Tables["Suppliers"]);
//Update the database's Products table to which the oleDbDataAdapter2 is connected
DoUpdate(oleDbDataAdapter2, dataSet11.Tables["Products"]);
}

public void DoUpdate(DbDataAdapter dataAdapter, System.Data.DataTable dataTable) {
try {
dataAdapter.Update(dataTable);
} catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}


标签:DevExpressGridView

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:小锋神的博客—博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP