How to use dropdown inside radgrid?

12 posts, 0 answers
  1. Tzach Kaufmann
    Tzach Kaufmann avatar
    95 posts
    Member since:
    Aug 2008

    Posted 02 Jun 2014 Link to this post

    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?





  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Jun 2014 in reply to Tzach Kaufmann Link to this post

    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

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tzach Kaufmann
    Tzach Kaufmann avatar
    95 posts
    Member since:
    Aug 2008

    Posted 05 Jun 2014 in reply to Princy Link to this post

    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?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Jun 2014 in reply to Tzach Kaufmann Link to this post

    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
  6. Tzach Kaufmann
    Tzach Kaufmann avatar
    95 posts
    Member since:
    Aug 2008

    Posted 05 Jun 2014 in reply to Princy Link to this post

    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
  7. Tzach Kaufmann
    Tzach Kaufmann avatar
    95 posts
    Member since:
    Aug 2008

    Posted 05 Jun 2014 in reply to Tzach Kaufmann Link to this post

    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?
  8. Tzach Kaufmann
    Tzach Kaufmann avatar
    95 posts
    Member since:
    Aug 2008

    Posted 05 Jun 2014 in reply to Tzach Kaufmann Link to this post

    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>
  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Jun 2014 in reply to Tzach Kaufmann Link to this post

    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
  10. Tzach Kaufmann
    Tzach Kaufmann avatar
    95 posts
    Member since:
    Aug 2008

    Posted 05 Jun 2014 in reply to Princy Link to this post

    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?
  11. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Jun 2014 in reply to Tzach Kaufmann Link to this post

    Hi,

    Please try the below sample code snippet, it works fine using InPlace edit. I have attached a screenshot of the output. Provide your complete code if this doesn't help.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" AllowPaging="True" runat="server" AllowSorting="true" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound">
        <MasterTableView EditMode="InPlace">
            <Columns>
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />
                <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
                <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" UniqueName="CustomerID" />
                <telerik:GridTemplateColumn DataField="ShipCountry" UniqueName="ShipCountry" HeaderText="ShipCountry" AllowFiltering="true" SortExpression="ShipCountry">
                    <ItemTemplate>
                        <%# Eval("ShipCountry") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadComboBox runat="server" ID="rcbShipCountry">
                        </telerik:RadComboBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");
    }
     
    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("rcbShipCountry");
            combo.DataSource = GetDataTable("SELECT distinct ShipCountry FROM Orders");
            combo.DataTextField = "ShipCountry";
            combo.DataValueField = "ShipCountry";
            combo.DataBind();
            combo.SelectedValue = DataBinder.Eval(editItem.DataItem, "ShipCountry").ToString();
        }
    }
     
    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);
        DataTable myDataTable = new DataTable();
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
        return myDataTable;
    }

    Thanks,
    Princy
  12. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Jun 2014 in reply to Princy Link to this post

    Hi,

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

    Thanks,
    Princy
  13. Meenakshi
    Meenakshi avatar
    3 posts
    Member since:
    Oct 2015

    Posted 24 Oct 2015 Link to this post

    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.​

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017