Binding DropDown inside RadGrid

13 posts, 0 answers
  1. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 22 Oct 2010 Link to this post

    hi,
    I am new in using Telerik radcontrol in asp.net.
    i am trying to create a order form in this form using a gridview in this grid view have dropdownlist,label and two textbox.
    first i bind to dropdown list that access data from sqldatabase.
    i am unable to bind that 
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Oct 2010 Link to this post

     Hello Shivesh,

    A better suggestion is to use 'GridDropDownColumn' where the following Demo describes how it works.
    http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

    If you want to customize that column, please go through the following documentation.
    Customize/Configure GridDropDownColumn

    You can also use DropDownList inside GridTemplateColumn.Then populate the control by setting its DataSourceID to SqlDataSource. Sample code is given below.

    Mark-Up:
    <telerik:GridTemplateColumn>
         <ItemTemplate>
           <asp:DropDownList  DataTextField="LastName" DataValueField="LastName" runat="server" DataSourceID="SqlDataSource1"></asp:DropDownList>
          </ItemTemplate>
    </telerik:GridTemplateColumn>

    Thanks,
    Princy
  3. Pavlina
    Admin
    Pavlina avatar
    6170 posts

    Posted 22 Oct 2010 Link to this post

    Hi Shivesh,

    I suggest that you examine the help topic below which discusses how to bind a dropdown control in EditItemTemplate:
    Operations with MS DropDownList in EditItemTemplate of GridTemplateColumn

    Kind regards,
    Pavlina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 22 Oct 2010 Link to this post

    Hi Princy,

    I have bind radgrid and try to bind dropdown but unable to do that my code is
    <telerik:RadGrid ID="radGrid1" runat="server" AutoGenerateColumns="False" GridLines="None"
                AllowMultiRowEdit="True" OnItemDataBound="radGrid1_DataBound" OnNeedDataSource="radGrid1_NeedDataSource">
                <MasterTableView AutoGenerateColumns="false">
                    <Columns>
                        <telerik:GridTemplateColumn HeaderText="Product Name">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" DataValueField="Product_Id"
                                    DataTextField="Product_Name">
                                </asp:DropDownList>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="Product_rate">
                            <ItemTemplate>
                                <asp:Label ID="lblrate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Product_Rate")%>'></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="Product Quantity">
                            <ItemTemplate>
                                <asp:TextBox ID="txtQuantity" runat="server" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="Product Amount">
                            <ItemTemplate>
                                <asp:TextBox ID="txtAmount" runat="server" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
    and my code behind is

    protected void radGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            {
                SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                SqlDataAdapter da = new SqlDataAdapter("select * from product_detail", connection);
                DataTable dt = new DataTable();
                connection.Open();
                try
                {
                    da.Fill(dt);
                }
                finally
                {
                    connection.Close();
                }
                radGrid1.DataSource = dt.DefaultView;
            }
     
     
     
     
    protected void radGrid1_DataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
    SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                SqlDataAdapter da = new SqlDataAdapter("select product_id,product_name from product_detail", connection);
                    DataSet ds=new DataSet();
                    da.Fill(ds);
     
                    GridEditableItem item = e.Item as GridEditableItem;
                    DropDownList list = item.FindControl("ddl1") as DropDownList;
                    list.DataSource = ds;
                    list.DataTextField = "product_name";
                    list.DataValueField = "product_id";
                    list.DataBind();
                    }
            }
    plz suggest me what is the problem??
  5. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 22 Oct 2010 Link to this post

    Hi,

    But Incase i have retrieving data from sqldatabase then how can fill dropdownlist.
    i do't want to use SqlDatSource ID.

    plz suggest me.

    thanks
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Oct 2010 Link to this post

    Hello Shivesh,

    I can't find any problem with your code. Could you check whether the DataSet is populating properly by puting breakpoint in the ItemDataBound event?
    I hope someone from Telerik will help us.


    Thanks,
    Princy.
  7. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 22 Oct 2010 Link to this post

    hi,
    here my item data bound code is
    protected void radGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        SqlConnection connection = new SqlConnection();
        connection = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand("select product_id,product_name from product_detail", connection);
     
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds, "product_detail");
        }
        finally
        {
            connection.Close();
        }
        radgrid1.DataSource = ds;
     
    }
     
    protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            DataRowView row = (DataRowView)e.Item.DataItem;
            item["ddl1"].Text = row["product_name"].ToString();
        }
    }
    problem is that this is running and value display in right column but all value like a simple column that are not showing in drop down column.
    <telerik:RadGrid ID="radgrid1" runat="server" OnNeedDataSource="radGrid1_NeedDataSource" OnItemDataBound="radGrid1_ItemDataBound" OnItemCreated="radGrid1_ItemCreated" AutoGenerateColumns="false">
            <MasterTableView AutoGenerateColumns="false" Width="100%" ShowHeader="true">
            <Columns>
            
            <telerik:GridDropDownColumn UniqueName="ddl1" ListTextField="Product_Name" ListValueField="Product_Id" ListDataMember="Product_Detail" DataField="Product_Id" HeaderText="Product_Name"></telerik:GridDropDownColumn>
            <telerik:GridBoundColumn HeaderText="Product_Rate" UniqueName=" Product_Rate" DataField="Product_Rate"></telerik:GridBoundColumn>
            </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    thanks that should be coming with droddown i do't know waht is the issue plz check??
  8. Pavlina
    Admin
    Pavlina avatar
    6170 posts

    Posted 27 Oct 2010 Link to this post

    Hi Shivesh,

    Please try using the following code and see if it helps to implement the desired functionality:
    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;
            GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("ddl1"));        
            RadComboBox combo = editor.ComboBoxControl;
            combo.AllowCustomText = true;
            combo.MarkFirstMatch = true;
            combo.DataSourse = //set the desired data source           
        }
    }

    All the best,
    Pavlina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Matt
    Matt avatar
    57 posts
    Member since:
    Jun 2012

    Posted 27 Nov Link to this post

    I databound a RadDropDownList to a SqlDataSource within a GridTemplateColumn and it worked until I allowed multiple row edit's. Unfortunately, Telerik doesn't test every possible scenario with their code and/or they expect you to be a Telerik developer and just "know" what combinations don't work - or require a "hack" with code-behind. This is one reason why my license was not renewed. This post is 7 years old and yet the problem still exists.
  10. Matt
    Matt avatar
    57 posts
    Member since:
    Jun 2012

    Posted 27 Nov Link to this post

    BTW, RadGrid1_ItemDataBound isn't being called at OnItemDataBound="RadGrid1_ItemDataBound" if AllowMultiRowEdit="true".
  11. Pavlina
    Admin
    Pavlina avatar
    6170 posts

    Posted 29 Nov Link to this post

    Hi Matt,

    I have prepared a sample runnable page with grid and dropdownlist where AllowMultiRowEdit property is set to true and ItemDataBound event fires as expected. Please refer to the video below in order to see how it works on my end. 
    https://www.screencast.com/t/dFb7S7TCSd

    Attached you can also find my test project. Please give it a try and let me know what is the difference on your end and if I missed something from your logic out.

    I hope this helps.

    Regards,
    Pavlina
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  12. Matt
    Matt avatar
    57 posts
    Member since:
    Jun 2012

    Posted 29 Nov in reply to Pavlina Link to this post

    The link returns a 403 error and when I follow the zip file example, no data is being appended to the control.

    Here is a sniplet:

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowEdit="true" EnableViewState="false" AllowFilteringByColumn="True" AllowPaging="True" PageSize="20" AllowAutomaticDeletes="true" AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowSorting="True" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="False" DataSourceID="SqlDataSource1" CellSpacing="0" GridLines="None" AutoGenerateColumns="false" Width="100%" style="margin:0px 10px 10px 10px" OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource">
    <ClientSettings EnableRowHoverStyle="true" AllowColumnsReorder="True" AllowKeyboardNavigation="true">
    <Selecting AllowRowSelect="True" />
    </ClientSettings>
    <MasterTableView EditMode="Batch" HorizontalAlign="NotSet" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowAutomaticDeletes="True" AllowAutomaticUpdates="True" CommandItemDisplay="Bottom" DataKeyNames="EmployeeID" Width="100%">
    <BatchEditingSettings EditType="Row" />
    <CommandItemSettings ShowAddNewRecordButton="true" ShowExportToExcelButton="false" AddNewRecordText="Add new associate"/>
    <Columns>

    <telerik:GridTemplateColumn DataField="StatusCode" HeaderText="Status Code" SortExpression="StatusCode" UniqueName="StatusCode" HeaderStyle-Width="75px">
    <FilterTemplate>
    <telerik:RadComboBox ID="RCB_StatusCode" runat="server" Width="50px" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("StatusCode").CurrentFilterValue %>' OnClientSelectedIndexChanged="TitleIndexChanged">
    <Items>
    <telerik:RadComboBoxItem Value="" Text="All"/>
    <telerik:RadComboBoxItem Value="A" Text="A" />
    <telerik:RadComboBoxItem Value="L" Text="L" />
    <telerik:RadComboBoxItem Value="P" Text="P" />
    <telerik:RadComboBoxItem Value="T" Text="T" />
    </Items>
    </telerik:RadComboBox>
    <telerik:RadScriptBlock ID="RadScriptBlock2" runat="server">
    <script type="text/javascript">
    function TitleIndexChanged(sender, args) {
    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
    tableView.filter("StatusCode", args.get_item().get_value(), "EqualTo");
    }
    </script>
    </telerik:RadScriptBlock>
    </FilterTemplate>
    <ItemTemplate>
    <%# Eval("StatusCode") %>
    </ItemTemplate>
    <EditItemTemplate>
    <telerik:RadComboBox ID="RCB_StatusCode" runat="server" Width="50px" AppendDataBoundItems="true">
    <Items>
    <telerik:RadComboBoxItem Value="" Text="All"/>
    </Items>
    </telerik:RadComboBox>
    <asp:RequiredFieldValidator ID="RFV_StatusCode" runat="server" ControlToValidate="RCB_StatusCode" Text="&#8666;" ForeColor="#E31932" Font-Bold="true" Font-Size="14pt" Display="Dynamic" SetFocusOnError="true" />
    </EditItemTemplate>
    </telerik:GridTemplateColumn>

    </Columns>
    </MasterTableView>

     

    And the code behind:

    private DataTable GetDataTable()
    {
    DataTable dataTable = new DataTable();
    using (SqlConnection sqlConnection = new SqlConnection(SqlConn.WB()))
    {
    SqlCommand sqlCommand = new SqlCommand("select distinct StatusCode from dbo.MyScheduleUsers order by StatusCode", sqlConnection);
    sqlCommand.CommandType = CommandType.Text;
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
    sqlDataAdapter.Fill(dataTable);
    }
    return dataTable;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
    if(e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
    GridEditableItem item = e.Item as GridEditableItem;
    RadComboBox RCB_StatusCode = item.FindControl("RCB_StatusCode") as RadComboBox;
    RCB_StatusCode.DataSource = GetDataTable();
    RCB_StatusCode.DataValueField = "StatusCode";
    RCB_StatusCode.DataTextField = "StatusCode";
    RCB_StatusCode.DataBind();
    }
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
    (sender as RadGrid).DataSource = GetDataTable();
    }

    So.... it's still not working....

  13. Rumen
    Admin
    Rumen avatar
    13276 posts

    Posted 04 Dec Link to this post

    Hi Matt,

    Please find attached a sample project using your own code and combobox registration and a *.mp4 video demonstrating the whole scenario.

    To be able to run the project on your end, you need to add the missing Telerik assemblies for .NET 4.5 in the bin folder.

    Let me know if you have any questions.

    Best regards,
    Rumen
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top