轉帖|其它|編輯:郝浩|2012-10-11 10:09:00.000|閱讀 690 次
概述:介紹如何在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); } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:小鋒神的博客—博客園