標籤:設計 提示 dex eth 處理 rgs out bind 字串
背景
需要在gridview中增加一列。之前的資料都是通過Datatable 綁定到GridControl.DataSource呈現。(GridControl 不能只通過非繫結資料行呈現)
Note that the Grid Control cannot operate with only unbound columns (without binding the control to a data source using the GridControl.DataSource and GridControl.DataMember properties)。
問題描述
在gridview中通道Columns.AddField()添加了一列(GridColumn),當對這一列的儲存格進行賦值時無效果。
賦值方式是通過gridView1.SetRowCellValue();
解決方案
響應gridView的CustomUnboundColumnData 訊息,其中CustomColumnDataEventArgs 包含以下屬性 IsSetData,IsGetData,Value等。
此訊息在gridview初始化,以及其它繫結資料行的資料被修改時被啟用,此時IsGetData為true,IsSetData為false;
對非繫結資料行的儲存格進行編輯(添加非繫結資料行以後預設是可編輯的)和對非繫結資料行進行SetRowCellValue()時訊息也被啟用,此時IsSetData為true,IsGetData為false;
在IsSetData為true的時候,改變Value的值對儲存格內呈現的值無效,該值不會被顯示到介面上。所以SetRowCellValue方法無法對非繫結資料行的儲存格進行賦值。
當IsGetData為true的時候,改變Value的值儲存格內的值也就改變了。
通常的,在CustomUnboundColumnData 訊息被響應的時候如果IsSetData 為true,該訊息會被再次響應,此時IsGetData為true;
所以要使非繫結資料行的編輯和SetRowCellValue()有效,代碼如下:
//以下代碼只適用於單行,因為資料只暫存在一個edit對象,當有多行的時候,每一行的資料都有可能變成edit
object edit; private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if(e.IsSetData) { edit = e.Value; }else if(e.IsGetData) { e.Value = edit; } }
問題尋找過程
當遇到SetRowCellValue()無效的時候,我首先想到的是屬性是否設定正確。
首先,非繫結資料行被建立時,visible 屬性為false。由此可見,非繫結資料行與繫結資料行相比,有很多屬性都是需要手動設定的。
依據以往的瞭解,決定儲存格資料顯示的有三個屬性:
ColumnType,列的類型
DisplayFormat.FormatType ,格式類型
DisplayFormat.FormatString,格式字串
後面兩個屬性在使用繫結資料行的時候我通常也會設定,所以已經設定好。
於是設定ColumnType,但是該屬性為唯讀類型,看來只適用於繫結資料行。
通過type關鍵字提示發現,存在UnboundType,預設情況下該值為None.於是設定為DevExpress.Data.UnboundColumnType.Decimal;
結果依然失敗。
Google後發現
需要響應gridView的CustomUnboundColumnData 訊息
總結
由於非繫結資料行在處理資料輸入時比較麻煩:訊息響應兩次,需要建立資料對象進行儲存,刪除時也需要刪除對應資料來源。所以不適合作為編輯列。
However, please keep in mind those situations in which unbound columns retrieve their data from a custom data source, and a record is added to or deleted from the grid‘s main data source. When a record is added, you usually need to add a new entry to the custom data source that corresponds to the new record in the grid. Similarly, when a record is deleted you usually need to delete the corresponding entry in the custom data source. To receive notifications about a record that has been added or removed, use the methods provided by the data source.
猜測
官方設計的非繫結資料行運用情境主要適用以下樣本情境:
官方範例程式碼:
using DevExpress.XtraGrid.Views.Base;using DevExpress.XtraGrid.Views.Grid;using DevExpress.XtraGrid.Columns;private void Form1_Load(object sender, System.EventArgs e) { // ... gridControl1.ForceInitialize(); // Create an unbound column. GridColumn unbColumn = gridView1.Columns.AddField("Total"); unbColumn.VisibleIndex = gridView1.Columns.Count; unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal; // Disable editing. unbColumn.OptionsColumn.AllowEdit = false; // Specify format settings. unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; unbColumn.DisplayFormat.FormatString = "c"; // Customize the appearance settings. unbColumn.AppearanceCell.BackColor = Color.LemonChiffon;}// Returns the total amount for a specific row.decimal getTotalValue(GridView view, int listSourceRowIndex) { decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "UnitPrice")); decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity")); decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Discount")); return unitPrice * quantity * (1 - discount);}// Provides data for the Total column.private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) { GridView view = sender as GridView; if (e.Column.FieldName == "Total" && e.IsGetData) e.Value = getTotalValue(view, e.ListSourceRowIndex);}
參考資料:
官方文檔:
https://documentation.devexpress.com/#WindowsForms/CustomDocument1477
同一問題:
https://stackoverflow.com/questions/20137808/how-to-set-a-value-to-unbound-column-cell-setrowcellvalue-grinview-winforms
C# winform dev gridcontrol 非繫結資料行(unbound)的使用