问题描述:

I've been struggling to find a simple example or guidance to my problem. Basically, I have one dataset. This table has three columns,

  • Document Name (string)
  • Document Owner (string)
  • Permission (int)

I want to bind this DataTable to a AspxGridView. However, I want the final column to be bound to a AspxComboBox so that:

  • If Permission = 1 then bind ComboBox with item A
  • If Permission = 2 then bind ComboBox with items A and B
  • If Permission = 3 then bind ComboBox with items A, B and C

How can I achieve this? A lot of the samples I've found talk about the AspxGridView being in edit mode. The point here is that I'm not actually editing the grid. All I want to do is do a postback on ComboBox change to do some action.

网友答案:

Just bind through SelectedValue property of dropdownList. Check below sample

Aspx
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="False" 
    onrowdatabound="gvTest_RowDataBound">
<Columns>
    <asp:TemplateField HeaderText="Serial No."><ItemTemplate><%# Container.DataItemIndex+1 %></ItemTemplate></asp:TemplateField>
    <asp:TemplateField HeaderText="Fruits" ><ItemTemplate><asp:DropDownList runat="server" ID="ddlFruits" SelectedValue='<%# Bind("FruitID") %>'   >
        <asp:ListItem Value="1">Apples</asp:ListItem>
        <asp:ListItem Value="2">Pineapples</asp:ListItem>
        <asp:ListItem Value="3">Banana</asp:ListItem>
        </asp:DropDownList></ItemTemplate> </asp:TemplateField>
</Columns>
</asp:GridView>

C#

protected void Page_Load(object sender, EventArgs e)
    {   
        gvTest.DataSource = GetData();
        gvTest.DataBind();
    }

    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("FruitID"));
        for (int i = 0; i < 3; i++)
        {
            DataRow dr=dt.NewRow();
            dr["FruitID"] = i + 1;
            dt.Rows.Add(dr);
        }
        return dt;
    }
网友答案:

Editable combo box in grid display (non-editing) mode:

<dx:GridViewDataColumn>
    <DataItemTemplate>
        <dx:ASPxComboBox runat="server" AutoPostBack="True" ...></dx:ASPxComboBox>
    </DataItemTemplate>
</dx:GridViewDataColumn>

ASPxEdit.AutoPostBack
GridViewDataColumn.DataItemTemplate

相关阅读:
Top