Example E3591
Visible to All Users

Grid View for ASP.NET Web Forms - How to bind the GridViewDataComboBoxColumn edit form editor at runtime

This example illustrates how to populate a GridViewDataComboBoxColumn with data and set its properties at runtime.

A grid with an edit form

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

Documentation

More Examples

Example Code

Solution/Default.aspx
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>
Solution/Default.aspx.cs(vb)
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; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.