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

Binding dropdown template column to BindingList value

1 Answer 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 21 Aug 2012, 08:14 PM
I'm binding a RadGrid to a BindingList, when I edit a Template column with a dropDown I'm trying  it to keep the previously selected value

here is a sample of my code:

<telerik:GridTemplateColumn HeaderText="Contributor" UniqueName="Contributor" DataField="Contrib">
    <EditItemTemplate>
        <asp:DropDownList ID="ddContributor" runat="server" DataSourceID="ldsContibutors"
        DataTextField="ContributorName" DataValueField="ContributorID" AppendDataBoundItems="True"
        SelectedValue='<%# Eval("Contrib.ContributorID") %>'>
            <asp:ListItem>Select a value</asp:ListItem>
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <%# Eval("Contrib.ContributorName") %>
    </ItemTemplate>


eval works fine but bind throws an exception: DataBinding: 'Telerik.Web.UI.GridInsertionObject' does not contain a property with the name 'Contrib'.


I also tried in cs but I cannot find the value in the item["Contributor"].Text 
then I tried witha a label in the Item template,  ((Label)item["Contributor"].findControl("labelID")).Text but it's always ""
any help will be apreciated thanks!

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Aug 2012, 05:37 AM
Hi Carlos,

I have made some modifications in your code. Put the DropDownList inside InsertItemTemplate in order to show it in insert mode. Try accessing the fields in ItemDataBound event as shown below.
aspx:
<telerik:GridTemplateColumn HeaderText="Contributor" UniqueName="Contributor" DataField="Contrib">
   <EditItemTemplate>
      <asp:DropDownList ID="ddContributor" runat="server" DataSourceID="ldsContibutors" DataTextField="ContributorName" DataValueField="ContributorID" AppendDataBoundItems="True" SelectedValue='<%# Eval("ContributorID") %>'>
          <asp:ListItem>Select a value</asp:ListItem>
       </asp:DropDownList>
    </EditItemTemplate>
  <InsertItemTemplate>
    <asp:DropDownList ID="ddContributor" runat="server" DataSourceID="ldsContibutors" DataTextField="ContributorName" DataValueField="ContributorID" AppendDataBoundItems="True" SelectedValue='<%# Eval("ContributorID") %>'>
         <asp:ListItem>Select a value</asp:ListItem>
    </asp:DropDownList>
   </InsertItemTemplate>
   <ItemTemplate>
        <%# Eval("ContributorName") %>
    </ItemTemplate>
</telerik:GridTemplateColumn>
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
   if (e.Item is GridDataItem)
   {
      string s = ((DataRowView)e.Item.DataItem)["ContributorName"].ToString();//accessing eval field
   }
   if (e.Item is GridEditableItem && e.Item.IsInEditMode)
   {
     GridEditableItem item = (GridEditableItem)e.Item;
     DropDownList ddl = (DropDownList)item.FindControl("ddContributor");//accessing dropdownlist
   }
}

Thanks,
Princy.
Tags
Grid
Asked by
Carlos
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or