This is a migrated thread and some comments may be shown as answers.

DropDown list inside a Radgrid Help

2 Answers 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sima
Top achievements
Rank 1
Sima asked on 27 Sep 2012, 07:36 PM
Hi,

I have a dropdown list inside the radgrid 

<telerik:GridTemplateColumn  HeaderText="" UniqueName="Answer" DataField="Answer"  >
                    <ItemTemplate>
                    <asp:DropDownList ID="ddlAnswer" runat="server"   DataTextField="Answer"
DataValueField="Answer" >
                    <asp:ListItem  Value="Yes" >Yes</asp:ListItem>
                    <asp:ListItem  Value="No" >No</asp:ListItem>
                    </asp:DropDownList>
                    </ItemTemplate>
                     </telerik:GridTemplateColumn>

If the user selects No and hits submit button the record will be updated in the database and below the answer No will be saved.(Database is updating properly)

When I click on this page again or refresh the page the page should be loaded with all the correct data from the database.

 every time the page loads all data except this drop down is getting binded properly. There are few records where the user selected No but still the drop down is showing Yes. How can I make No as the selected item?

2 Answers, 1 is accepted

Sort by
0
Sima
Top achievements
Rank 1
answered on 27 Sep 2012, 09:17 PM
Any one?
0
Accepted
Kostadin
Telerik team
answered on 01 Oct 2012, 04:18 PM
Hi Sima,

A possible approach is to get the value form the database and set it as value of the DropDownList in OnItemDataBound event handler.
Mark Up:
<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1"
        GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound">
        <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
          <%-- <telerik:GridBoundColumn DataField="CompanyName" FilterControlAltText="Filter CompanyName column"
                    HeaderText="Company Name" SortExpression="CompanyName" UniqueName="CompanyName">
                </telerik:GridBoundColumn>--%>
                <telerik:GridTemplateColumn HeaderText="Company Name Template Column" UniqueName="CNTemplate">
                    <ItemTemplate>
                        <asp:DropDownList ID="DDL" runat="server" DataSourceID="SqlDataSource1" DataTextField="CompanyName">
                        </asp:DropDownList>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT [CompanyName] FROM [Customers]"></asp:SqlDataSource>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        DropDownList ddl = item["CNTemplate"].FindControl("DDL") as DropDownList;
        string value = (item.DataItem as DataRowView)["CompanyName"].ToString();
        ddl.Items.FindByText(value).Selected = true;
    }
}


All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Sima
Top achievements
Rank 1
Answers by
Sima
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or