How to set selected value of an asp:dropdownlist in a AspxGridView

1.8k views Asked by At

I checked around for a solution to this and the solutions I have found use a SqlDataSource, but I am not populating it that way. I have the items hardcoded in the html, and the grid is binded to a dataset. This is my first time using dropdowns in any type of grid and I am getting confused. I tried using the ComboBoxColumn in the item template but was having major issues on trying to find the combobox control, so I went with the normal asp:DropDownList. Incase you're wondering, I can't even find that control without it returning null.

So as the grid gets populated I need to set the selected value of the dropdowns.

The markup is

<dx:ASPxGridView ID="xgvEdit" runat="server" Width="100%">
<Columns>
    <dx:GridViewDataColumn FieldName="roleID" Caption="ID" Visible="false"></dx:GridViewDataColumn>
    <dx:GridViewDataColumn FieldName="modulID" Caption="Document/UseCase (Right Object)">
        <Settings AutoFilterCondition="Contains" />
    </dx:GridViewDataColumn>
    <dx:GridViewDataColumn FieldName="right_level" Caption="Right Level">
        <DataItemTemplate>
            <asp:DropDownList ID="ddRightLevel" runat="server" AutoPostBack="false">
                <asp:ListItem Text="No Right" Value="0" />
                <asp:ListItem Text="Read" Value="1" />
                <asp:ListItem Text="Write" Value="2" />
                <asp:ListItem Text="Execute" Value="3" />
            </asp:DropDownList>
        </DataItemTemplate>
    </dx:GridViewDataColumn>
    <dx:GridViewDataColumn FieldName="comments" Caption="Comments">
        <Settings AutoFilterCondition="Contains" />
    </dx:GridViewDataColumn>
</Columns>

1

There are 1 answers

8
mybirthname On BEST ANSWER

I suppose that this is custom GridView which inherits asp:GridView. You need to add event

OnRowDataBound="Grid_RowDataBound"

In Code behind:

    protected void ProductGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem == null)
            return;


       DropDownList ddl= e.Row.FindControl("ddRightLevel") as DropDownList;
       //do stuff
     }