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

How to use dropdown inside radgrid?

10 Answers 992 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Covertix
Top achievements
Rank 1
Covertix asked on 02 Jun 2014, 02:10 PM
I want to have a column in my rad grid that contains dropdown list.

I added (based on your examples):

<telerik:RadGrid ID="gvWebUsers" runat="server" OnNeedDataSource="gvWebUsers_NeedDataSource" OnItemCreated="gvWebUsers_ItemCreated"
                    Skin="Gray" AutoGenerateColumns="false"
                    OnItemDataBound="gvWebUsers_ItemDataBound" AllowAutomaticUpdates="true"
                    AllowPaging="true" CssClass="SettingsGrid" 
                    Width="99.7%">
                    
                    <ClientSettings Resizing-AllowColumnResize="true" Resizing-ClipCellContentOnResize="true" />
                    
                    <MasterTableView DataKeyNames="UserID" PageSize="15" EditMode="InPlace" >
                        <PagerStyle Mode="NextPrevAndNumeric" />
                        <Columns>
                             <telerik:GridBoundColumn DataField="EmailAddress" HeaderText="<%$ Resources:English,Settings_MobilityUsers_Email %>"
                                UniqueName="EmailAddress" />
                                
                             <telerik:GridBoundColumn DataField="FirstName" HeaderText="<%$ Resources:English,Settings_MobilityUsers_FirstName %>"
                                UniqueName="FirstName" />
                                
                             <telerik:GridBoundColumn DataField="LastName" HeaderText="<%$ Resources:English,Settings_MobilityUsers_LastName %>"
                                UniqueName="LastName" />
                        
                             <telerik:GridBoundColumn DataField="IsDomainUser" HeaderText="<%$ Resources:English,Settings_MobilityUsers_IsDomainUser %>"
                                UniqueName="IsDomainUser" />
                                
                             <telerik:GridBoundColumn DataField="IsVerified" HeaderText="<%$ Resources:English,Settings_MobilityUsers_IsVerified %>"
                                UniqueName="IsVerified" />
                                
                             <telerik:GridButtonColumn HeaderStyle-Width="80px" ItemStyle-Width="80px" ButtonType="LinkButton" UniqueName="BlockButton" />
                             
                             <telerik:GridButtonColumn HeaderStyle-Width="80px" ItemStyle-Width="80px" ButtonType="LinkButton" UniqueName="ChangePassButton" />

                            <telerik:GridButtonColumn HeaderStyle-Width="80px" ItemStyle-Width="80px" ButtonType="LinkButton" UniqueName="AllowUploadButton" />
                             
                             <telerik:GridButtonColumn HeaderStyle-Width="80px" ItemStyle-Width="80px" ButtonType="LinkButton" UniqueName="UnlockButton" />




                    <telerik:GridTemplateColumn HeaderText="Category" ItemStyle-Width="240px">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem, "IsUploadAllowed")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadComboBox runat="server" ID="RadComboBox2" DataTextField="IsUploadAllowed"
                                DataValueField="IsUploadAllowed" SelectedValue='<%#Bind("IsUploadAllowed") %>'>
                            </telerik:RadComboBox>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="EditCommandColumn"
                        HeaderText="Edit" HeaderStyle-Width="100px" UpdateText="Update">
                    </telerik:GridEditCommandColumn>

                            
                            
                        </Columns>
                        
                    </MasterTableView>
                </telerik:RadGrid>

But when I press on "Update" I get an error.
In addition, how can I bind data to the rad combobox from server side? (like itemdatabound)? Is there any way to call server side function on update command?





10 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Jun 2014, 05:14 AM
Hi,

Please add a DataSourceID to the RadComboBox, you can bind it from server side in the ItemDataBound event.

ASPX:
<telerik:GridTemplateColumn HeaderText="Category" >
  <EditItemTemplate>
    <telerik:RadComboBox ID="RadComboBox2" DataSourceID="SqlDataSource2" . . .>
    </telerik:RadComboBox>
  </EditItemTemplate>
</telerik:GridTemplateColumn>

​      OR

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
    GridEditableItem editItem = (GridEditableItem)e.Item;
    RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBox2");
    combo.DataSourceID = "";// set DataSource
    combo.DataTextField = "IsUploadAllowed";
    combo.DataValueField = "IsUploadAllowed";
    //To set the current selected value
    combo.SelectedValue = DataBinder.Eval(editItem.DataItem, "IsUploadAllowed").ToString();
  }
}

Thanks,
Princy

0
Covertix
Top achievements
Rank 1
answered on 05 Jun 2014, 06:30 AM
Thanks Princy!
Is there any other way to do it?
I use asp.net and my connection string is saved in Web.Config.
There's no way to bind data table, list or dictionary?
0
Princy
Top achievements
Rank 2
answered on 05 Jun 2014, 07:17 AM
Hi,

You can bind the RadComboBox to your required datasource using the DataSource property. Take a look at the sample code snippet.

C#:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindnewConnectionString3"].ToString());
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        String queryString = "SELECT distinct ShipName from Orders";
        con.Open();
        SqlDataAdapter dr = new SqlDataAdapter(queryString, con);
        DataTable dt = new DataTable();
        dr.Fill(dt);
        con.Close();
        GridEditableItem editItem = (GridEditableItem)e.Item;
        RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBox2");
        combo.DataSource = dt;// binding to datatable
        combo.DataTextField = "ShipName";
        combo.DataValueField = "ShipName";
        combo.DataBind();   
        combo.SelectedValue = DataBinder.Eval(editItem.DataItem, "ShipName").ToString(); 
    }
}

Thanks,
Princy
0
Covertix
Top achievements
Rank 1
answered on 05 Jun 2014, 07:22 AM
Hi Princy, 
I made the change you suggested, but I still get an exception ehwn clicking on "Edit" inside the grid:

Line: 513
Error: Sys.WebForms.PageRequestManagerServerErrorException: Selection out of range
Parameter name: value
0
Covertix
Top achievements
Rank 1
answered on 05 Jun 2014, 07:29 AM
I solved this issue :)

But now I have another issue:
1 - how can I edit only the "rad combo" column and not the entire row?
2 - Can I call my own server side method on "Update" command?
0
Covertix
Top achievements
Rank 1
answered on 05 Jun 2014, 07:36 AM
3 - The solution you gave me for data bind is not working, because the item is not GridEditableItem, I don't know why. Can you please take a look again?


<telerik:GridTemplateColumn HeaderText="Category" ItemStyle-Width="240px">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem, "IsUploadAllowed")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadComboBox runat="server" ID="RadComboBox2" DataTextField="IsUploadAllowed"
                                DataValueField="IsUploadAllowed" SelectedValue='<%#Bind("IsUploadAllowed") %>'>
                            </telerik:RadComboBox>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridEditCommandColumn FooterText="EditCommand footer" UniqueName="EditCommandColumn"
                        HeaderText="Edit" HeaderStyle-Width="100px" UpdateText="Update">
                    </telerik:GridEditCommandColumn>
0
Princy
Top achievements
Rank 2
answered on 05 Jun 2014, 09:58 AM
Hi,

The condition (e.Item is GridEditableItem && e.Item.IsInEditMode) will be true only when the grid is in edit or insert mode, so make sure you have the edit form open.

Thanks,
Princy
0
Covertix
Top achievements
Rank 1
answered on 05 Jun 2014, 10:07 AM
Hi Princy,

I want to use  EditMode="InPlace" and not editform.

Is there any way to bind items to radcombobox from server-side in this mode?
0
Princy
Top achievements
Rank 2
answered on 06 Jun 2014, 03:53 AM
Hi,

The attachment didn't come in the previous post. Adding it again.

Thanks,
Princy
0
Meenakshi
Top achievements
Rank 1
answered on 24 Oct 2015, 07:42 AM

Hi Team,

My requirement is I have 2 dropdownlist inside Radgrid. What I want to do is By selecting one dropdownlist ,need to load 2nd dropdown values. 

 protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string test;
                DataTable dt_LocationData = new DataTable();
                DataSet DS_GridData = new DataSet();
                DropDownList ddlVehicleList = (DropDownList)sender;
                //foreach (GridDataItem gvRow in GrdEmergencyDetails.Items)
                //{
                //    DropDownList ddlLocation = (DropDownList)gvRow.FindControl("ddlcmpLocation");
                //    ddlLocation.Items.Clear();
                //}

 

after selecting company name need to load locations dropdown. How to accomplish this code. Please help urgent.​

Tags
Grid
Asked by
Covertix
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Covertix
Top achievements
Rank 1
Meenakshi
Top achievements
Rank 1
Share this question
or