This example illustrates how to populate a GridViewDataComboBoxColumn with data and set its properties at runtime.
Use the GridViewDataComboBoxColumn.PropertiesComboBox property to access and customize column editor settings:
C#protected void Page_Load(object sender, EventArgs e) {
var comboColumn = ((GridViewDataComboBoxColumn)grid.Columns["CategoryID"]);
comboColumn.PropertiesComboBox.DataSource = dsCombo;
comboColumn.PropertiesComboBox.TextField = "CategoryName";
comboColumn.PropertiesComboBox.ValueField = "CategoryID";
comboColumn.PropertiesComboBox.ValueType = typeof(Int32);
}
Note: The ComboBoxProperties.ValueType should be set according to the Data Type Mappings (ADO.NET) table.
Files to Look At
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
- ASPxGridView - How to bind GridViewDataComboBoxColumn Edit Form editor at runtime
- How to edit data in a DataTable using ASPxGridView at runtime when data is stored in ViewState
- ASPxGridView - Batch Edit mode - How to remove already selected ComboBox column items
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Solution.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to bind GridViewDataComboBoxColumn at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="grid" runat="server" OnCustomErrorText="grid_CustomErrorText" AutoGenerateColumns="False" DataSourceID="dsGrid"
KeyFieldName="ProductID"
onrowinserting="grid_RowInserting" onrowupdating="grid_RowUpdating">
<Columns>
<dx:GridViewCommandColumn VisibleIndex="0" ShowEditButton="true" ShowNewButton="true">
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="1">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="SupplierID" VisibleIndex="3">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="QuantityPerUnit" VisibleIndex="4">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="5">
</dx:GridViewDataTextColumn>
<dx:GridViewDataComboBoxColumn FieldName="CategoryID" VisibleIndex="6">
</dx:GridViewDataComboBoxColumn>
</Columns>
</dx:ASPxGridView>
<asp:AccessDataSource ID="dsGrid" runat="server" DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice] FROM [Products]">
</asp:AccessDataSource>
<asp:AccessDataSource ID="dsCombo" runat="server" DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:AccessDataSource>
</div>
</form>
</body>
</html>
C#using DevExpress.Web;
using System;
namespace Solution {
public class CallbackException : Exception {
public CallbackException(string message)
: base(message) {
}
}
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
var comboColumn = ((GridViewDataComboBoxColumn)grid.Columns["CategoryID"]);
comboColumn.PropertiesComboBox.DataSource = dsCombo;
comboColumn.PropertiesComboBox.TextField = "CategoryName";
comboColumn.PropertiesComboBox.ValueField = "CategoryID";
comboColumn.PropertiesComboBox.ValueType = typeof(Int32);
}
protected void grid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
throw new CallbackException("Data modifications are not allowed in the online example.");
}
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
throw new CallbackException("Data modifications are not allowed in the online example.");
}
protected void grid_CustomErrorText(object sender, ASPxGridViewCustomErrorTextEventArgs e) {
if (e.Exception is CallbackException)
e.ErrorText = e.Exception.Message;
}
}
}