简体   繁体   English

如何填充ASPxComboBox?

[英]How to populate ASPxComboBox?

I have Editable ASPxGridView and confused how to populate the ASPxComboBox init. 我有可编辑的ASPxGridView,并且困惑如何填充ASPxComboBox初始化。

Consider The following scenario in which we have a list of cars with colors. 考虑以下场景,在该场景中,我们列出了具有颜色的汽车。

初始GridView

After Edit is clicked. 单击编辑后。

按下“编辑”按钮后的GridView

I want to add datasource to this highlighted color combobox. 我想将数据源添加到此突出显示的颜色组合框中。 My code is given below: 我的代码如下:

ASP Code ASP代码

<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" 
KeyFieldName="ID" onstartrowediting="ASPxGridView1_StartRowEditing">
<Columns>
    <dx:GridViewCommandColumn VisibleIndex="0">
        <EditButton Visible="True">
        </EditButton>
    </dx:GridViewCommandColumn>
    <dx:GridViewDataTextColumn Caption="ID" FieldName="ID" Name="ID" 
        VisibleIndex="1">
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataTextColumn Caption="Car" FieldName="Car" Name="Car" 
        VisibleIndex="2">
        <EditItemTemplate>
               <dx:ASPxComboBox ID="ASPxComboBox1" runat="server" 
                           Text='<%# Eval("Car") %>'>
               </dx:ASPxComboBox>
        </EditItemTemplate>
    </dx:GridViewDataTextColumn>
    <dx:GridViewDataTextColumn Caption="Color" FieldName="Color" Name="Color" 
        VisibleIndex="3">
        <EditItemTemplate>
            <dx:ASPxComboBox ID="colorCombo" runat="server">
            </dx:ASPxComboBox>
        </EditItemTemplate>
    </dx:GridViewDataTextColumn>
</Columns>

C# Code C#代码

protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Car");
            dt.Columns.Add("Color");

            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Car"] = "Suzuki";
            dr["Color"] = "Green";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "2";
            dr["Car"] = "Toyota";
            dr["Color"] = "Blue";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "3";
            dr["Car"] = "Toyota";
            dr["Color"] = "Black";
            dt.Rows.Add(dr);

            grid.DataSource = dt;
            grid.DataBind();
        }

        protected void ASPxGridView1_StartRowEditing(object sender,
                          DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Color");

            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Color"] = "Green";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "2";
            dr["Color"] = "Blue";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "3";
            dr["Color"] = "Black";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "4";
            dr["Color"] = "Red";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "5";
            dr["Color"] = "Yellow";
            dt.Rows.Add(dr);


            ASPxComboBox cb=(ASPxComboBox)grid.FindEditRowCellTemplateControl(
                                 grid.Columns["Color"] as GridViewDataColumn  
                                , "colorCombo");
            cb.DataSource = dt;
            cb.DataBind();

        }

After putting break point on cb.Datasource = dt; 将断点放在cb.Datasource = dt之后; it is verified that values are populated but not viewed on the page. 确认已填充值,但未在页面上查看。 This populating of combobox cannot be done on page_load(). 无法在page_load()上完成组合框的填充。 If anyone know the solution kindly tell me in easy and simple words. 如果有人知道解决方案,请用简单的语言告诉我。

NOTE: Datasource is coming from database , here I just hardcoded it in Pageload() . 注意: Datasource来自database ,这里我只是将其硬编码在Pageload()

You can do this with an XML datasource. 您可以使用XML数据源执行此操作。 That way you can avoid the code behind, populate the list and display the current color selection. 这样,您可以避免后面的代码,填充列表并显示当前的颜色选择。 You will need to convert the GridViewDataTextColumn to a GridViewDataComboBoxColumn and then add the XML datasource to it. 您将需要将GridViewDataTextColumn转换为GridViewDataComboBoxColumn,然后将XML数据源添加到其中。 Too add an xml datasource just right click on the App_Data folder and select Add->New Item. 只需右键单击App_Data文件夹并选择Add-> New Item,就可以添加xml数据源。 Select an xml file and name it colors.xml. 选择一个xml文件,并将其命名为colors.xml。 Code below: 代码如下:

XML: XML:

<colors>
  <item ID="1" Color="Green"></item>
  <item ID="2" Color="Blue"></item>
  <item ID="3" Color="Black"></item>
  <item ID="4" Color="Red"></item>
  <item ID="5" Color="Yellow"></item>
</colors>

Lines to add to ASPX: 要添加到ASPX的行:

<asp:XmlDataSource ID="colorsXML" runat="server" DataFile="~/App_Data/colors.xml" XPath="colors/item" ></asp:XmlDataSource>

Change: 更改:

<dx:GridViewDataTextColumn Caption="Color" FieldName="Color" Name="Color" 
    VisibleIndex="3">
    <EditItemTemplate>
        <dx:ASPxComboBox ID="colorCombo" runat="server">
        </dx:ASPxComboBox>
    </EditItemTemplate>
</dx:GridViewDataTextColumn>

To this: 对此:

<dx:GridViewDataComboBoxColumn Caption="Color" FieldName="Color" Name="Color" VisibleIndex="3">
<PropertiesComboBox DataSourceID="colorsXML" ValueField="ID" TextField="Color"></PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>

Wipe out the code in the StartRowEditing function, you won't need it: 清除StartRowEditing函数中的代码,您将不需要它:

protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
{

}

I go with the approach shown by dcreight. 我接受dcreight显示的方法。

If you are using objectdata source for binding your combobox you can modify the code suggested by him as follows: 如果您使用对象数据源来绑定组合框,则可以按以下方式修改他建议的代码:

 <dx:GridViewDataComboBoxColumn Caption="Color" FieldName="ID" Name="Color" VisibleIndex="3">
<PropertiesComboBox DataSourceID="objColors" ValueField="ID" TextField="Color" ValueType="System.String"></PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>

Make sure the fieldname and valuefield of propertiescombobox match. 确保propertiescombobox的字段名和值字段匹配。 Also Define the objectdatasource in your code outside the gridview 还要在网格视图之外的代码中定义objectdatasource

  <asp:ObjectDataSource ID="objColors" SelectMethod="GetColors" TypeName="ClassFileName"
 runat="server"></asp:ObjectDataSource>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM