RadDropDownList datasource inside RadGrid

11 posts, 0 answers
  1. YMA
    YMA avatar
    5 posts
    Member since:
    Sep 2006

    Posted 22 Jul 2013 Link to this post

    Hi,

    For the last few weeks I have been trying to populate the datasource of a raddropdownlist inside a radgridcolumn -but without success.  I have looked at various examples online and in the help files -but none of them seem to work for me. 

    I thought that maybe one of you knew why I can't get my code to populate the dropdownlist.


    The problem seems to be that (e.Item is GridEditableItem && e.Item.IsEditMode) is never true in my grid and therefore never evaluates the code inside the if statement that would bind the dropdownlist... Thanks in advance. YMA

    Here is an example of the grid and the dropdownlist (CategoryIDDropDown) that I can't populate :

    private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            // access/modify the edit item template settings here
            DropDownList list = item.FindControl("CategoryIDDropDown") as DropDownList;
            list.DataSource = mydatasource;
            list.DataBind();
    
    {
    
    {



    <telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"

                AllowAutomaticInserts="True" PageSize="10" Skin="Default" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted"

                OnItemUpdated="RadGrid1_ItemUpdated" AllowAutomaticUpdates="True" AllowPaging="True"

                AutoGenerateColumns="False" OnBatchEditCommand="RadGrid1_BatchEditCommand" DataSourceID="SqlDataSource1" Height="500px" OnItemCommand="RadGrid1_ItemCommand" Visible="True">

                <ExportSettings>

                    <Pdf>

                        <PageHeader>

                            <LeftCell Text="" />

                            <MiddleCell Text="" />

                            <RightCell Text="" />

                        </PageHeader>

                        <PageFooter>

                            <LeftCell Text="" />

                            <MiddleCell Text="" />

                            <RightCell Text="" />

                        </PageFooter>

                    </Pdf>

                </ExportSettings>

                <MasterTableView AutoGenerateColumns="False" BatchEditingSettings-EditType="Row" CommandItemDisplay="Bottom" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" EditMode="Batch" HorizontalAlign="NotSet">

                    <EditFormSettings>

                        <EditColumn FilterControlAltText="Filter EditCommandColumn column">

                        </EditColumn>

                    </EditFormSettings>

                    <BatchEditingSettings EditType="Cell" />

                    <SortExpressions>

                        <telerik:GridSortExpression FieldName="ProductID" SortOrder="Descending" />

                    </SortExpressions>

                    <CommandItemSettings ExportToPdfText="Export to PDF" />

                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">

                    </RowIndicatorColumn>

                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">

                    </ExpandCollapseColumn>

                    <Columns>

                        <telerik:GridBoundColumn DataField="ProductName" HeaderStyle-Width="210px" HeaderText="ProductName" SortExpression="ProductName" UniqueName="ProductName">

                            <ColumnValidationSettings EnableRequiredFieldValidation="true">

                                <RequiredFieldValidator ForeColor="Red" Text="*This field is required">

                                </RequiredFieldValidator>

                                <ModelErrorMessage Text="" />

                            </ColumnValidationSettings>

                            <HeaderStyle Width="210px" />

                        </telerik:GridBoundColumn>

                        <telerik:GridTemplateColumn DataField="CategoryID" HeaderStyle-Width="180px" HeaderText="Category" UniqueName="CategoryID">

                            <ItemTemplate>

                                <%# Eval("CategoryName") %>

                            </ItemTemplate>

                            <EditItemTemplate>

                                <telerik:RadDropDownList ID="CategoryIDDropDown" runat="server" DataTextField="CategoryName" DataValueField="CategoryID">

                                </telerik:RadDropDownList>

                            </EditItemTemplate>

                            <HeaderStyle Width="180px" />

                        </telerik:GridTemplateColumn>

                        <telerik:GridNumericColumn ColumnEditorID="NumericEditor1" DataField="UnitsInStock" HeaderStyle-Width="80px" HeaderText="Units In Stock" SortExpression="UnitsInStock" UniqueName="UnitsInStock">

                            <ColumnValidationSettings>

                                <ModelErrorMessage Text="" />

                            </ColumnValidationSettings>

                            <HeaderStyle Width="80px" />

                        </telerik:GridNumericColumn>

                        <telerik:GridBoundColumn ColumnEditorID="TextEditor" DataField="QuantityPerUnit" HeaderStyle-Width="135px" HeaderText="Quantity Per Unit" SortExpression="QuantityPerUnit" UniqueName="QuantityPerUnit">

                            <ColumnValidationSettings>

                                <ModelErrorMessage Text="" />

                            </ColumnValidationSettings>

                            <HeaderStyle Width="135px" />

                        </telerik:GridBoundColumn>

                        <telerik:GridCheckBoxColumn DataField="Discontinued" HeaderStyle-Width="80px" HeaderText="Discontinued" SortExpression="Discontinued" UniqueName="Discontinued">

                            <HeaderStyle Width="80px" />

                        </telerik:GridCheckBoxColumn>

                        <telerik:GridTemplateColumn ColumnEditorID="NumericEditor1" DataField="UnitPrice" HeaderStyle-Width="80px" HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="TemplateColumn">

                            <ItemTemplate>

                                <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label>

                            </ItemTemplate>

                            <EditItemTemplate>

                                <span>

                                <telerik:RadNumericTextBox ID="tbUnitPrice" runat="server" Width="55px">

                                </telerik:RadNumericTextBox>

                                <span style="color: Red">

                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbUnitPrice" ErrorMessage="*Required">

                                        </asp:RequiredFieldValidator>

                                </span></span>

                            </EditItemTemplate>

                            <HeaderStyle Width="80px" />

                        </telerik:GridTemplateColumn>

                        <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmDialogType="RadWindow" ConfirmText="Delete this product?" ConfirmTitle="Delete" HeaderStyle-Width="50px" HeaderText="Delete" Text="Delete" UniqueName="DeleteColumn">

                            <HeaderStyle Width="50px" />

                        </telerik:GridButtonColumn>

                    </Columns>

                    <PagerStyle PageSizeControlType="RadComboBox" />

                </MasterTableView>

                <HeaderStyle BackColor="Silver" ForeColor="White" />

                <PagerStyle PageSizeControlType="RadComboBox" />

                <FilterMenu EnableImageSprites="False">

                </FilterMenu>

            </telerik:RadGrid>

            <telerik:GridNumericColumnEditor ID="NumericEditor1" runat="server">

                <NumericTextBox ID="NumericTextBox1" runat="server" Width="60px" />

            </telerik:GridNumericColumnEditor>

            <telerik:GridTextBoxColumnEditor runat="server" ID="TextEditor">

                <TextBoxStyle Width="120px" />

            </telerik:GridTextBoxColumnEditor>

        </telerik:RadAjaxPanel>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

            DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [CategoryID], [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock]) VALUES (@ProductName, @CategoryID, @UnitPrice, @Discontinued, @QuantityPerUnit, @UnitsInStock)"

            SelectCommand="SELECT [ProductID], [ProductName], [Products].[CategoryID], [Categories].[CategoryName] as CategoryName, [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock] FROM [Products] JOIN Categories ON Products.CategoryID=Categories.CategoryID"

            UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID, [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued, [QuantityPerUnit] = @QuantityPerUnit, [UnitsInStock] = @UnitsInStock WHERE [ProductID] = @ProductID">

            <DeleteParameters>

                <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>

            </DeleteParameters>

            <InsertParameters>

                <asp:Parameter Name="ProductName" Type="String"></asp:Parameter>

                <asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>

                <asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>

                <asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>

                <asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>

                <asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>

            </InsertParameters>

            <UpdateParameters>

                <asp:Parameter Name="ProductName" Type="String"></asp:Parameter>

                <asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>

                <asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>

                <asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>

                <asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>

                <asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>

                <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>

            </UpdateParameters>

        </asp:SqlDataSource>

        

  2. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 25 Jul 2013 Link to this post

    Hello,

    Note that you could not add additional items to the DropDownColumn when using a batch editing. Batch editing functionality enables inline client-side editing and performing multiple changes before the user decides to either apply the changes or cancel them. In this case the condition will never be true on ItemDataBound. I would recommend you to use a TemplateColumn with RadComboBox as an EditItemTemplate and add additional items on the client. Please check out the following help article where is demonstrated how to work with items in client side.

    Regards,
    Kostadin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. YMA
    YMA avatar
    5 posts
    Member since:
    Sep 2006

    Posted 25 Jul 2013 Link to this post

    Thanks Kostadin,

    II am not trying to add additional items -I just want to data bind the DropDownList to my own datasource (i.e. even when grid is not set to batch edit mode) in the code-behind instead of setting its datasource = "SqlDataSource2" at design time.

    Here is an example: http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/batchediting/defaultcs.aspx

    Note that the datasource of the CategoryIDDropDown is set to SqlDataSource2.

    <telerik:GridTemplateColumn HeaderText="Category" HeaderStyle-Width="180px" UniqueName="CategoryID" DataField="CategoryID">
                            <ItemTemplate>
                                <%# Eval("CategoryName") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadDropDownList runat="server"ID="CategoryIDDropDown" DataValueField="CategoryID"
                                    DataTextField="CategoryName"DataSourceID="SqlDataSource2">
                                </telerik:RadDropDownList>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>


    Question:
    How would the code-behind look like if the CategoryIDDropDown was databound via code instead of setting the datasource at design time.

    I.e. instead of DataSourceID="SqlDataSource2"? I need something like this:
    CategoryIDDropDown.DataSource = "same_data_as_SqlDataSource2";
    CategoryIDDropDown.DataBind();



    Thanks

    YMA
  4. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 31 Jul 2013 Link to this post

    Hi,

    In order to set a DataSource you have to hook on PreRender event handler and find the DropDown editor and set the data source of the RadDropDownList . Check out the following code snippet.
    Mark-Up:
    <telerik:GridTemplateColumn HeaderText="Category" HeaderStyle-Width="180px" UniqueName="CategoryID" DataField="CategoryID">
                            <ItemTemplate>
                                <asp:Label Text='<%# Eval("CategoryName") %>' runat="server" /> 
                            </ItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadDropDownList runat="server" ID="CategoryIDDropDown" >
                                </telerik:RadDropDownList>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
    C#:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        RadDropDownList dropDownList = RadGrid1.FindControl(RadGrid1.MasterTableView.ClientID + "_CategoryID").FindControl("CategoryIDDropDown") as RadDropDownList;
        dropDownList.DataSource = GetDataSource();
        dropDownList.DataBind();
    }
     
    private List<string> GetDataSource()
    {
        List<string> list = new List<string>();
        list.Add("Item1");
        list.Add("Item2");
        list.Add("Item3");
        list.Add("Item4");
        list.Add("Item5");
     
        return list;
    }

    Regards,
    Kostadin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  5. YMA
    YMA avatar
    5 posts
    Member since:
    Sep 2006

    Posted 01 Aug 2013 Link to this post

    Your solution works like a charm -many thanks!!!

    Regards
    YMA
  6. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 13 Apr 2015 in reply to YMA Link to this post

    If I use a Template Column, the auto-updating feature of the Batch mode won't work - unless I'm missing something?

    I just need to add a blank row to GridDropDownColumn so the Batch mode doesn't think I want to pick a value (I need to support not picking a value from the list of options in the EntityDataSource). 

  7. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 16 Apr 2015 Link to this post

    Hi Peter,

    I am afraid I am not completely understand your requirement. How did you add a black row? What do you mean by saying "I need to support not picking a value from the list of options"? Are you trying to set the columns as read only? I would appreciate if you can provide your code declaration and the related code behind in order to better understand your scenario.

    Regards,
    Kostadin
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  8. Chen-yi
    Chen-yi avatar
    1 posts
    Member since:
    Jan 2012

    Posted 09 Nov 2015 in reply to Kostadin Link to this post

    Hi Kostadin,

    How can we call server side function/methods in batch editing?

    I want to call it to load the values of a RadComboBox.

    Thanks!

  9. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 11 Nov 2015 Link to this post

    Hi Chen-yi,

    A possible solution is to use LoadOnDemand. Please check out the attached sample which demonstrates how to achieve that.

    Regards,
    Kostadin
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  10. Upender
    Upender avatar
    4 posts
    Member since:
    Oct 2017

    Posted 27 Aug 2018 Link to this post

    Hi Team, I got one different situation like, based upon one  cell value in rad grid, need to populate dropdownlist in batch edit mode. then i am facing strange issue. here i have used client events to do achieve this task.Please find the attached file for more details and please do the needful.

    used these below events:

    <ClientEvents OnBatchEditOpened="OnBatchEditOpened" OnRowClick="GetRoleValue" OnBatchEditSetEditorValue="SetEditorValue" />

  11. Eyup
    Admin
    Eyup avatar
    3647 posts

    Posted 30 Aug 2018 Link to this post

    Hello Upender,

    For this scenario you can use the InPlace edit mode instead:
    https://demos.telerik.com/aspnet-ajax/dropdowntree/examples/applicationscenarios/dropdowntreeingrid/defaultcs.aspx?product=grid
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/edit-mode/in-place


    I am afraid this requirement is not supported with Batch editing mode, which is different than other server-side modes:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-batch-edit-mode

    You can check the samples provided in the following post:
    https://www.telerik.com/forums/radcombobox-cascading-inside-radgrid#GxsIQqyyPkqj1Za-mNbalg

    I hope this will prove helpful.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top