0

I'm trying to use ASPxGridView to display a list of ASPxComboBox controls. Both the rows in the grid and the list of options in the combo boxes are populated from code. I'm having problems setting the initial value of the combo boxes.

I'm looking for it to look similar to the image below.

enter image description here

As you can see in the screenshot, I have been able to get both the grid view & the combo boxes to populate, but I can't figure out how to set the initial values of the combo boxes.

  • In the Init event of the inner combo boxes, there's no obvious property to retrieve the bound object.
  • I did find a couple other questions on StackOverflow, for which the answer was to add a bound property to the combo box. However, adding SelectedIndex='<%# Bind("Level") %>' to the declaration for InnerCombo gave me the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

Here's what I have so far:

Testing.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Light.master" 
    AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="MyProject.Testing" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <dx:ASPxGridView ID="MyGridView" runat="server" KeyFieldName="Name">
        <Columns>
            <dx:GridViewDataColumn FieldName="Name" />
            <dx:GridViewDataColumn FieldName="Level">
                <DataItemTemplate>
                    <dx:ASPxComboBox
                        runat="server"
                        ID="InnerCombo"
                        ValueField="ID"
                        TextField="Desc"
                        ValueType="System.Int32"
                        OnInit="InnerCombo_Init" />
                </DataItemTemplate>
            </dx:GridViewDataColumn>
        </Columns>
    </dx:ASPxGridView>

    <dx:ASPxButton runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" />
</asp:Content>

Testing.aspx.cs:

public partial class Testing : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
     if (!this.IsPostBack)
     {
        this.MyGridView.DataSource = GetDefaultSettings();
        this.MyGridView.DataBind();
     }
  }

  protected void btnSubmit_Click(object sender, EventArgs e)
  {
     Debug.WriteLine("btnSubmit_Click");

     for (int i = 0; i < MyGridView.VisibleRowCount; i++)
     {
        object[] row = (object[])MyGridView.GetRowValues(i, "Name", "Value");

        // row[1] is null, but we can get the data by finding the combo box itself.
        GridViewDataColumn col = (GridViewDataColumn)MyGridView.Columns["Value"];
        ASPxComboBox innerCombo = (ASPxComboBox)MyGridView.FindRowCellTemplateControl(i, col, "InnerCombo");

        Debug.WriteLine("{0} = {1}", row[0], innerCombo.Value);
     }
  }

  protected void InnerCombo_Init(object sender, EventArgs e)
  {
     Debug.WriteLine("InnerCombo_Init");

     ASPxComboBox innerCombo = sender as ASPxComboBox;
     if (innerCombo != null)
     {
        innerCombo.DataSource = GetValues();
        innerCombo.DataBind();
     }
  }

  private static List<ValueClass> GetValues()
  {
     // Simple for testing; actual method will be database access.
     return new List<ValueClass>()
     {
        new ValueClass(0, "Zero (default)"),
        new ValueClass(1, "One"),
        new ValueClass(2, "Two"),
        new ValueClass(3, "Three"),
     };
  }

  private static List<SettingClass> GetDefaultSettings()
  {
     // Simple for testing; actual method will be database access + post-processing.
     return new List<SettingClass>()
     {
        new SettingClass("AA", 0),
        new SettingClass("BB", 1),
        new SettingClass("CC", 0),
     };
  }

  public class ValueClass
  {
     public int ID { get; private set; }
     public string Desc { get; private set; }

     public ValueClass(int id, string desc)
     {
        this.ID = id;
        this.Desc = desc;
     }
  }

  public class SettingClass
  {
     public string Name { get; set; }
     public int Value { get; set; }

     public SettingClass(string name, int value)
     {
        this.Name = name;
        this.Value = value;
     }
  }
}
David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • Not familiar with ASPx controls, but this works for me for traditional asp DropDownLists: `SelectedValue='<%# Bind("YourValueField") %>` – agileMike Oct 26 '15 at 22:10
  • I tried that, but it said "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." I think the `DataItemTemplate` is set up properly, but perhaps it needs to be different to work with data binding. – David Yaw Oct 27 '15 at 15:28
  • This one of many pitfalls of using devexpress controls. They are just not usable in real world scenarios. – user6694745 Nov 02 '20 at 12:54

0 Answers0