RadGrid with RadComboBox in batch edit mode

13 posts, 0 answers
  1. Alfonso
    Alfonso avatar
    19 posts
    Member since:
    Jul 2013

    Posted 01 Oct 2014 Link to this post

    Hi,

    Telerik controls version: 2014.2.724.45

    I have a RadGrid with EditMode="Batch". This RadGrid has a GridTemplateColumn with a RadComboBox in EditItemTemplate. If a description with a special character (<, >, ...) appears in RadComboBox, OldValues in BatchEditCommand has an incorrect value.

    Modify Comment field and click Save button. In NewValues["ID_SLOT"] you can see value 4,5 (incorrect).

    Another issue OldValues["ID_SLOT"] is not present.

    <telerik:RadCodeBlock ID="mainRadCodeBlock" runat="server">
                <script type="text/javascript">
                    function saveChanges() {
                        var grid = $find("<%=GridExample.ClientID %>");
                    grid.get_batchEditingManager().saveChanges(grid.get_masterTableView());
                }
     
                </script>
            </telerik:RadCodeBlock>
     
            <asp:Button ID="btnSave" runat="server" Text="Save" ToolTip="Save changes to database" OnClientClick="saveChanges();return false;" />
     
            <telerik:RadGrid ID="GridExample" runat="server" AutoGenerateColumns="false"
                Skin="Simple"
                AllowPaging="False"
                AllowSorting="false"
                AllowFilteringByColumn="true"
                OnBatchEditCommand="GridExample_BatchEditCommand"
                OnNeedDataSource="GridExample_NeedDataSource">
                <GroupingSettings CaseSensitive="false" />
                <ClientSettings>
                    <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true" />
                    <Selecting AllowRowSelect="true" />
                </ClientSettings>
                <MasterTableView HeaderStyle-HorizontalAlign="Center" Width="100%" AutoGenerateColumns="false"
                    DataKeyNames="ID_COUNTRY"
                    EditMode="Batch">
                    <BatchEditingSettings EditType="Cell" OpenEditingEvent="MouseOver" />
                    <Columns>
                        <telerik:GridBoundColumn DataField="COMMENTS" HeaderText="Comments" HeaderTooltip="Comments" HeaderStyle-Width="100px" ItemStyle-Width="100px" AutoPostBackOnFilter="true" ShowFilterIcon="false" FilterControlWidth="100%"></telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn AutoPostBackOnFilter="true" ShowFilterIcon="false" FilterControlWidth="100%" UniqueName="SLOT" HeaderText="Slot" DataType="System.Int32" DataField="ID_SLOT" SortExpression="SLOT" ColumnGroupName="Devices" HeaderStyle-Width="150px" ItemStyle-Width="150px">
                            <ItemTemplate>
                                <%# Eval("SLOT") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="ddlSLOT" runat="server"
                                    EnableLoadOnDemand="true"
                                    Filter="Contains"
                                    AllowCustomText="false"
                                    DataTextField="SLOT"
                                    DataValueField="ID_SLOT"
                                    OnItemsRequested="ddlSLOT_ItemsRequested"
                                    Style="width: 135px;" class="caronte-portfolio-ddl-slot">
                                </telerik:RadComboBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>


    protected void GridExample_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID_COUNTRY", typeof(int));
            table.Columns.Add("COMMENT", typeof(string));
            table.Columns.Add("ID_SLOT", typeof(int));
            table.Columns.Add("SLOT", typeof(string));
     
            DataRow row = null;
            for (int i = 0; i < 5; i++)
            {
                row = table.NewRow();
                row["ID_COUNTRY"] = i;
                row["ID_SLOT"] = 1;
                row["SLOT"] = "Flagship < 4,5\" SP";
     
                table.Rows.Add(row);
            }
     
            (sender as RadGrid).DataSource = table;
        }
     
        protected void ddlSLOT_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID_SLOT", typeof(int));
            table.Columns.Add("SLOT", typeof(string));
     
            DataRow row = table.NewRow();
            row["ID_SLOT"] = 1;
            row["SLOT"] = "Flagship < 4,5\" SP";
     
            table.Rows.Add(row);
     
            RadComboBox ddlSlot = sender as RadComboBox;
            ddlSlot.DataSource = table;
            ddlSlot.DataBind();
        }
        protected void GridExample_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
        {
            foreach (GridBatchEditingCommand command in e.Commands)
            {
                 
            }
        }


    Thanks in advance.

    Alfonso
  2. Alfonso
    Alfonso avatar
    19 posts
    Member since:
    Jul 2013

    Posted 06 Oct 2014 in reply to Alfonso Link to this post

    Please, some help with this issue?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 06 Oct 2014 Link to this post

    Hi Alfonso,

    The problem in the illustrated setup is that the combo in the EditItemTemplate uses load on demand with one item and it wont actually request items. This proves problematic as later the grid can not find the matching item and use its value.

    In order to resolve the problem you can disable the load on demand as demonstrated in the code provided below:

    ASPX:
    <telerik:RadCodeBlock ID="mainRadCodeBlock" runat="server">
                <script type="text/javascript">
                    function saveChanges() {
                        var grid = $find("<%=GridExample.ClientID %>");
                        grid.get_batchEditingManager().saveChanges(grid.get_masterTableView());
                    }
     
                </script>
            </telerik:RadCodeBlock>
      
            <asp:Button ID="btnSave" runat="server" Text="Save" ToolTip="Save changes to database" OnClientClick="saveChanges();return false;" />
      
            <telerik:RadGrid ID="GridExample" runat="server" AutoGenerateColumns="false"
                Skin="Simple"
                 OnPreRender="GridExample_PreRender"
                AllowPaging="False"
                AllowSorting="false"
                AllowFilteringByColumn="true"
                OnBatchEditCommand="GridExample_BatchEditCommand"
                OnNeedDataSource="GridExample_NeedDataSource">
                <GroupingSettings CaseSensitive="false" />
                <ClientSettings>
                    <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true" />
                    <Selecting AllowRowSelect="true" />
                </ClientSettings>
                <MasterTableView HeaderStyle-HorizontalAlign="Center" Width="100%" AutoGenerateColumns="false"
                    DataKeyNames="ID_COUNTRY"
                    EditMode="Batch">
                    <BatchEditingSettings EditType="Cell"  />
                    <Columns>
                        <telerik:GridBoundColumn DataField="COMMENTS" HeaderText="Comments" HeaderTooltip="Comments" HeaderStyle-Width="100px" ItemStyle-Width="100px" AutoPostBackOnFilter="true" ShowFilterIcon="false" FilterControlWidth="100%"></telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn AutoPostBackOnFilter="true" ShowFilterIcon="false" FilterControlWidth="100%" UniqueName="SLOT" HeaderText="Slot" DataType="System.Int32" DataField="ID_SLOT" SortExpression="SLOT" ColumnGroupName="Devices" HeaderStyle-Width="150px" ItemStyle-Width="150px">
                            <ItemTemplate>
                                <%# Eval("SLOT") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="ddlSLOT" runat="server"
                                     
                                    Filter="Contains"
                                    AllowCustomText="false"
                                    DataTextField="SLOT"
                                    DataValueField="ID_SLOT"
                                  
                                    Style="width: 135px;" class="caronte-portfolio-ddl-slot">
                                </telerik:RadComboBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>

    C#:
    protected void GridExample_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID_COUNTRY", typeof(int));
            table.Columns.Add("COMMENT", typeof(string));
            table.Columns.Add("ID_SLOT", typeof(int));
            table.Columns.Add("SLOT", typeof(string));
     
            DataRow row = null;
            for (int i = 0; i < 5; i++)
            {
                row = table.NewRow();
                row["ID_COUNTRY"] = i;
                row["ID_SLOT"] = i;
                row["SLOT"] = "Flagship < 4,5\" SP"+i.ToString();
     
                table.Rows.Add(row);
            }
     
            (sender as RadGrid).DataSource = table;
        }
     
       
        protected void GridExample_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
        {
            foreach (GridBatchEditingCommand command in e.Commands)
            {
     
            }
        }
     
        protected void GridExample_PreRender(object sender, EventArgs e)
        {
            RadComboBox combo=(GridExample.MasterTableView.GetBatchEditorContainer("SLOT") as Panel).FindControl("ddlSLOT") as RadComboBox;
            DataTable table = new DataTable();
            table.Columns.Add("ID_SLOT", typeof(int));
            table.Columns.Add("SLOT", typeof(string));
     
            DataRow row = null;
            for (int i = 0; i < 5; i++)
            {
                row = table.NewRow();
                row["ID_SLOT"] = i;
                row["SLOT"] = "Flagship < 4,5\" SP" + i.ToString();
     
                table.Rows.Add(row);
            }
            combo.DataSource = table;
            combo.DataBind();
        }

    Another option would be to try and force the RadComboBox to request its items once the cell is opened for edit. Bear in mind however that this approach only has meaning if the combo will hold a different subset of data for the different rows.

    Regards,
    Angel Petrov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Alfonso
    Alfonso avatar
    19 posts
    Member since:
    Jul 2013

    Posted 06 Oct 2014 in reply to Angel Petrov Link to this post

    Thank you for your help.

    I have activated load on demand because values in this combo depends on value selected on another column (value is stored in a hidden field and used in ddlSLOT_ItemsRequested (this is not shown becuase this code is a simplified version for showing the issue).

    All is working fine except when description contains special character (<, >, ', ...). If description doesn't contain special characters correct value is stored in NewValues and it can be used in GridExample_BatchEditCommand.


    Thanks again.

    Alfonso
  6. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 09 Oct 2014 Link to this post

    Hello Alfonso,

    I have modified the code so that the combo uses load on demand but did not observe any abnormalities in the control behavior. In my case I was able to display and obtain the values correctly as you can see from this video.

    That said the problems seems related to something specific in the setup. Could you please ensure that the application uses the latest version of the controls? Additionally I would like to ask you to try and modify the attachment in such a manner so that the problem can be replicated. That would greatly facilitate us in finding its true cause.

    Regards,
    Angel Petrov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Alfonso
    Alfonso avatar
    19 posts
    Member since:
    Jul 2013

    Posted 09 Oct 2014 in reply to Angel Petrov Link to this post

    Hi Angel,

    But the problem is when I modify Comment field and save as you can see in this video

    As you can see ID_SLOT is not present in OldValues and its value in NewValues is 4,5

    Telerik controls version: 2014.2.724.45

    Thank you for your help,

    Alfonso
  8. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 13 Oct 2014 Link to this post

    Hello Alfonso,

    Set DataType="System.String" to your column if you like to use values like "Flagship < 4,5\" SP5".
    With the current type of your column in the value is removed all characters that are not numeric, and the remaining 4,5 is tested to be int, it fails and falls down to decimal. In the end you get decimal value.

    Changing the DataType to String will correct this.

    If you need to bind to integer column in your database, and show string in combination with batch editing, you will need to use 2 separate columns, or to get the value from your combo box manually, by accessing directly the combo box, instead of using the command.NewValues.

    One additional solution that is generally not suggested to be used is getting the original arguments using reflection:

    List<object> arguments = GetInstanceField(typeof(GridBatchEditingCommand), command, "arguments") as List<object>;


    internal static object GetInstanceField(Type type, object instance, string fieldName)
    {
        BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
        FieldInfo field = type.GetField(fieldName, bindFlags);
        return field.GetValue(instance);
    }

    This will not work if your server runs in Medium Trust.

    Regards,
    Vasil
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Alfonso
    Alfonso avatar
    19 posts
    Member since:
    Jul 2013

    Posted 13 Nov 2014 in reply to Vasil Link to this post

    Thanks for your help.

    But documentation says "Another key element of the batch editing mode is the use of only one editor for a single column", so if you cannot access to combobox, because there is a combobox for the column, not for each cell, is this right?
  10. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 18 Nov 2014 Link to this post

    Hello Alfonso,

    Indeed when the EditMode is set to Batch the grid will render only one editor for the entire column and reuse it to edit the different cells in it. If you want to access this editor on the server you can use the GetBatchEditorContainer or GetBatchColumnEditor method. A sample demonstration of the use of these methods can be found in this help article.

    Regards,
    Angel Petrov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  11. Yogesh
    Yogesh avatar
    2 posts
    Member since:
    Dec 2015

    Posted 13 Oct in reply to Angel Petrov Link to this post

    The link is broken. Can you please update the link?

    Thanks.

  12. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 14 Oct Link to this post

    Hello yogesh,

    The links in this thread seems to be working. Maybe the site was just updating when you tried to open some.

    Regards,
    Vasil
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  13. Yogesh
    Yogesh avatar
    2 posts
    Member since:
    Dec 2015

    Posted 14 Oct in reply to Vasil Link to this post

    Nope. It is not working. Try to click on BatchEditingSample.zip sample link in thread. It is broken. In fact most of the sample code links in various forum posts are not working.
  14. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 14 Oct Link to this post

    Hello yogesh,

    I am uploading the sample again. It appears that multiple links are broken, and we are working on fix it globally.

    Regards,
    Vasil
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017