0

我正在尝试使用ASPxGridView来显示ASPxComboBox控件列表。网格中的行和组合框中的选项列表均由代码填充。我在设置组合框的初始值时遇到问题。

我正在寻找它看起来类似于下图。

在此处输入图像描述

正如您在屏幕截图中看到的那样,我已经能够同时填充网格视图和组合框,但我不知道如何设置组合框的初始值。

  • 在内部组合框的 Init 事件中,没有明显的属性可以检索绑定的对象。
  • 我确实在 StackOverflow 上找到了其他几个问题,答案是向组合框添加绑定属性。但是,添加SelectedIndex='<%# Bind("Level") %>'到声明中InnerCombo给了我错误“Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。”

这是我到目前为止所拥有的:

测试.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>

测试.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;
     }
  }
}
4

0 回答 0