Background
You need to add a column to the GridView. The previous data is bound to the Gridcontrol.datasource rendering through a DataTable. (Gridcontrol cannot be rendered only through unbound columns)
Note that the Grid Control cannot operate with only unbound columns (without binding the control to a data source using th E Gridcontrol.datasource and Gridcontrol.datamember properties).
Problem description
In the GridView Channel Columns.addfield () adds a column (GridColumn) that has no effect when assigning a value to a cell in this column.
The method of assignment is through Gridview1.setrowcellvalue ();
Workaround
Responds to the Customunboundcolumndata message of the GridView, where Customcolumndataeventargs contains the following properties Issetdata,isgetdata,value, and so on.
This message is activated when the GridView is initialized and the data of other bound columns is modified, at which point isgetdata is true andIssetdata is false;
The message is also activated when the cell for the Unbound column is edited (the default is editable after adding the unbound column) and Setrowcellvalue () on the unbound column, at which point issetdata is true,isgetdata is false;
When Issetdata is true, changing the value of value invalidates the value rendered in the cell, and the value is not displayed on the interface. Therefore, the Setrowcellvalue method cannot assign a value to a cell that is not bound to a column.
When Isgetdata is true, the value in the cell that changes value is changed.
Normally, if issetdata is true when the Customunboundcolumndata message is being responded to, the message will be responded again, at which point the isgetdata is true;
So to make the edit and Setrowcellvalue () of the unbound columns valid, the code is as follows:
The following code applies only to a single line, because the data only exists for one edit object, and when there are multiple rows, the data of each row may become an 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; } }
Problem Lookup Process
When encountering Setrowcellvalue () is invalid, the first thing I think about is whether the property is set correctly.
First, when the unbound column is created, the Visible property is false. Thus, there are many properties that need to be set manually compared to bound columns.
Based on previous knowledge, there are three properties that determine the display of cell data:
ColumnType, type of column
Displayformat.formattype, Format type
displayformat.formatstring, format string
The next two properties are usually set when using bound columns, so I've set them up.
The ColumnType is set, but the property is read-only and appears to apply only to bound columns.
The type keyword indicates that there is a unboundtype, which is none by default. Then set to DevExpress.Data.UnboundColumnType.Decimal;
The result still failed.
Google found after
Need to respond to the customunboundcolumndata message of the GridView
Summarize
Because the unbound column is cumbersome to handle data entry: The message responds two times, requires a new data object for storage, and deletes the corresponding data source when it is deleted. Therefore, it is not suitable as an edit column.
However, 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 was added, you usually need to add a new entry to the custom data source this corresponds to the new record I n the grid. Similarly, when a record was deleted you usually need to delete the corresponding entry in the custom data source. To receive notifications about a record of that have been added or removed, use the methods provided by the data source.
Guess
The official design of the non-binding column usage scenario is primarily applicable to the following example scenarios:
Official Sample code:
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 U Nitprice = 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);}
Resources:
Official documents:
https://documentation.devexpress.com/#WindowsForms/customdocument1477
Same issue:
Https://stackoverflow.com/questions/20137808/how-to-set-a-value-to-unbound-column-cell-setrowcellvalue-grinview-winforms
Use of C # WinForm Dev Gridcontrol unbound columns (unbound)