This example shows how to bind MVC GridView Extension columns to the following data sources:
- The ADO.NET DataTable - DataTableDataBinding View / DataTableDataBindingPartial Partial View
- The Typed List (List<T>) - TypedListDataBinding View / TypedListDataBindingPartial Partial View
Also, this example demonstrates how to create an unbound column and populate it with data.
Use the GridViewSettings.Columns property to access the collection of grid columns.
Add four columns and bind them to the "ID", "Text", "Quantity", "Price" data source fields:
settings.Columns.Add("ID");
settings.Columns.Add("Text");
settings.Columns.Add("Quantity");
settings.Columns.Add("Price");
Then, add an unbound column whose values are calculated based on the values of bound columns. Use the unbound column's FieldName and UnboundType properties to specify the column name and value type. Calculate column values in the CustomUnboundColumnData event handler:
settings.Columns.Add(unboundColumn => {
unboundColumn.FieldName = "UniqueFieldName";
unboundColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
});
settings.CustomUnboundColumnData = (sender, e) => {
if (e.Column.FieldName == "UniqueFieldName") {
int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
decimal price = (decimal)e.GetListSourceFieldValue("Price");
e.Value = quantity * price;
}
};
- Model.cs (VB: Model.vb)
- DataTableDataBinding.cshtml
- DataTableDataBindingPartial.cshtml
- TypedListDataBinding.cshtml
- TypedListDataBindingPartial.cshtml
(you will be redirected to DevExpress.com to submit your response)