Setting dropdown in ItemBound

5 posts, 0 answers
  1. sho
    sho avatar
    24 posts
    Member since:
    Mar 2007

    Posted 04 Aug 2011 Link to this post

    Hi,

    I am populating the radgrid by attaching a datasource where I have a dropdownlist in it.  My code goes something like mentioned below.

    the value of the designation for the datasource from database has something like L1, L2, L3........

    So I want to set the dropdown for the designation of the employee .

    Ex :
    EmployeeID=1
    EmployeeName="safar"
    EmpDesignation="L2"

    So the dropdown should set to Level2 for the employeeid 1 and so on. I assume it can be done in ItemBound.

             <telerik:RadGrid ID="EmployeeGrid" runat="server"
                        AutoGenerateColumns="False" Width="455px"
                        onitemdatabound="EmployeeGrid_ItemDataBound" >
                        <ClientSettings AllowDragToGroup="True">
                            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                            <Selecting AllowRowSelect="True"></Selecting>
                        </ClientSettings>
                        <MasterTableView AllowFilteringByColumn="False">
                            <Columns>
                                <telerik:GridBoundColumn ReadOnly="true" ItemStyle-Width="150px"  HeaderText="Employee Id"
                                 DataField="EmployeeId"><ItemStyle Width="150px" ></ItemStyle>
                                 </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn ReadOnly="true"  HeaderText="Employee Name"
                                DataField="EmployeeName" ItemStyle-Width="150px"><ItemStyle Width="150px"></ItemStyle>
                                </telerik:GridBoundColumn>
                                <telerik:GridTemplateColumn HeaderText ="Designation" Visible="true">
                                    <ItemTemplate>
                                         <asp:DropDownList ID="DesignationDropdownList" runat="server" >
                                            <asp:ListItem Text="Level1" Value="1"/>
                                            <asp:ListItem Text="Level2" Value="2"/>
                                            <asp:ListItem Text="Level3" Value="3"/>
                                            <asp:ListItem Text="Level4" Value="4"/>
                                         </asp:DropDownList>
                             </ItemTemplate>
                                </telerik:GridTemplateColumn>
                            </Columns>
                        </MasterTableView>

                    </telerik:RadGrid>
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 04 Aug 2011 Link to this post

    Hello sho,

    <MasterTableView AllowFilteringByColumn="False" DataKeyNames="Designation">
    Write below code inside itemDataBoundEvent
    if(e.item is GridDataItem)
    {
       GridDataItem item = (GridDataItem)e.item;
       DropDownList DesignationDropDownList = (DropDownList)item.FindControl("DesignationDropDownList");
         if(!String.IsNullorEmpty(Convert.ToString(item.GetDataKeyValue("Designation")))
         {
           DesignationDropDownList.SelectedValue = item.GetDataKeyValue("Designation").ToString();
         }
    //   DesignationDropDownList.SelectedText = item.GetDataKeyValue("Designation").ToString();
     
    }


    let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Aug 2011 Link to this post

    Hello sho,

    Try the following code snippet to populate DropDownList in ItemDataBound event.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridDataItem )
      {
        GridDataItem item = (GridDataItem)e.Item;
        DropDownList drp = (DropDownList)item.FindControl("DesignationDropdownList");
        drp.DataSourceID = "SqlDataSource1";
        drp.DataTextField="Designation";
        drp.DataValueField = "Designation";
        drp.DataBind();
      }
    }

    Thanks,
    Shinu
  5. sho
    sho avatar
    24 posts
    Member since:
    Mar 2007

    Posted 04 Aug 2011 Link to this post

    Hi Jayesh,

    Thanks for the reply.

    You are setting the DataKeyNames="Designation" in MasterTableView. My problem is if I have more than one dropdown what will I do?
    Is it possible to find the any DataField value which is bind to the current row so that I can set the item selected?


    Thanks,
    Aradhya
  6. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 04 Aug 2011 Link to this post

    Hello Aradhya,

    Method 1 using DataKey:
     you can take multiple DataKeyNames also.

    DataKeyNames="ID,Name,Designation"

    string Id =  item.GetDataKeyValue("ID").ToString();

    string Name = item.GetDataKeyValue("Name").ToString();
    string Designation = item.GetDataKeyValue("Designation").ToString();



    Method 2 : find the any DataField value which is bind to the current row

    GridDataItem item = (GridDataItem)e.item;
     string id  = item["ID"].Text;             NOTE :  ID is column unique name


    Method 3 : find the any DataField value which is "NOT" bind to the current row
    GridDataItem item = e.Item as GridDataItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, item );
            string id = newValues["AddressID"].ToString();


    Thanks,
    Jayesh Goyani
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017