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

Setting dropdown in ItemBound

4 Answers 54 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sho
Top achievements
Rank 1
sho asked on 04 Aug 2011, 06:12 AM
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>

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Aug 2011, 06:37 AM
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
0
Shinu
Top achievements
Rank 1
answered on 04 Aug 2011, 06:57 AM
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
0
sho
Top achievements
Rank 1
answered on 04 Aug 2011, 10:03 AM
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
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Aug 2011, 10:30 AM
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
Tags
Grid
Asked by
sho
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 1
sho
Top achievements
Rank 1
Share this question
or