0
votes

I have been trying to add a dropdown control in a column of a telerik grid view. Below is the code snippet. when I use the Find Control on dropdown, it returns null and the data is not bound.

ASP

<telerik:GridTemplateColumn AllowFiltering="False" ShowFilterIcon="False">
                            <HeaderStyle HorizontalAlign="Center" />
                            <HeaderTemplate>
                               Change Sort Order
                            </HeaderTemplate>
                            <ItemStyle HorizontalAlign="Center" Width="5%"/>
                            <ItemTemplate>
                                <asp:DropDownList ID="myDropDown" DataTextField="TextFieldValue" DataValueField="ValueFieldValue" runat="server"/>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>

C# Code

protected void gdvFMclevel1_ItemDataBound(object sender, GridItemEventArgs e)
        {
                GridItem item = (GridItem)e.Item;
                DropDownList list=(DropDownList)item.FindControl("myDropDown");
                Controller c = new Controller();
                DataSet ds = new DataSet();
                ds = c.GetSortList();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    list.DataSource = ds.Tables[0];
                    list.DataTextField = "Order";
                    list.DataValueField = "SortOrder";
                    list.DataBind();
                }
               }

Please let me know how can I get the data populated in the dropdown. I am getting an object reference exception on this.

1
DropDownList list=(DropDownList)item.FindControl("myDropDown"); this line is returning null for the "list".Garvit Dixit

1 Answers

0
votes

Add a DataSourceID="dataSource" to the dropdown. Declare a ObjectDataSource (ODS) on the page. Link the ODS to a function that returns the dataset. The function should be public.

public DataSet dataSource()
{
    DataSet ds = new DataSet();
    ds = c.GetSortList();

    return ds;
}

<telerik:GridTemplateColumn AllowFiltering="False" ShowFilterIcon="False">
    <HeaderStyle HorizontalAlign="Center" />
    <HeaderTemplate>
       Change Sort Order
    </HeaderTemplate>
    <ItemStyle HorizontalAlign="Center" Width="5%"/>
    <ItemTemplate>
        <asp:DropDownList ID="myDropDown" DataSourceID="odsDataSource" 
        DataTextField="Order" 
        DataValueField="SortOrder" 
        runat="server"/>
    </ItemTemplate>
</telerik:GridTemplateColumn>

<asp:ObjectDataSource runat="server" ID="odsDataSource"
        SelectMethod="dataSource"
        TypeName="yournamespace.yourclassName"></asp:ObjectDataSource>