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

Radgrid BatchEditCommand - Release Q3 2014 - problem to get data of newly added row

4 Answers 178 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kavitha
Top achievements
Rank 1
Kavitha asked on 04 Nov 2014, 03:33 PM
I have a RadGrid in which I associated EditBatchCommand event.
In this event , I must do insert/update/delete.
I have 2 buttons in my RadGrid page, insert button and update button, insert button will add new row and update button will update all changes.

I have associated client events as follows to these buttons, 

  if (UpdateButton.Visible)
                {
                    script = @"function SaveChanges_" + grid.ClientID + @"(sender,args) {
            var grid = $find('" + grid.ClientID + @"');
            var changes = grid.get_batchEditingManager()._extractChangesString(grid.get_masterTableView())
            if(changes.length > 0)
                grid.get_batchEditingManager().saveChanges(grid.get_masterTableView());
        }";
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "saveGrid", script, true);                    UpdateButton.AutoPostBack = false;
                    UpdateButton.OnClientClicked = "SaveChanges_" + grid.ClientID;
                    this.Page.Form.DefaultButton = UpdateButton.UniqueID;                } 
        
       if (InsertButton.Visible)
                {
                    script = @"function InsertRow_" + grid.ClientID + @"(sender,args) {
            var grid = $find('" + grid.ClientID + @"');
            grid.get_batchEditingManager().addNewRecord(grid.get_masterTableView());
        }";
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "insertRow", script, true);
                    InsertButton.AutoPostBack = false;
                    InsertButton.OnClientClicked = "InsertRow_" + grid.ClientID;
                }

I have updated 1 line in my grid.
In my event "BatchEditCommand", I am having 1 commands with type "update" and I am able to get values with command.NewValues.
But when I insert a line in my grid.
In my event "BatchEditCommand", I am not getting type "insert". I am having only one command with type "update" with command.NewValues = null.
I am having this problem only after updating to new version Q3 2014. The old version of Telerik worked well and I had a command of type "insert" and I was able to get the data of newly added row with command.NewValues.
How can get the datas of my newly added row?
Thanks in advance. 

4 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 07 Nov 2014, 08:10 AM
Hi Kavitha,

Although that we have solved the problem in the support ticket that you have opened for the same issue, I will post the answer here as well, hoping that it will help others with similar problem:

"After going through your current implementation I was able to isolate the root of the problem. Actually, what is causing the insert command to be converted to update command is the calling of the private _extractChangesString method, which internal logic was modified in the latest version.

With the latest version, a new public method have been exposed for determining if there are any changes within a TableView - the BatchEditingManager hasChanges(tableView), which will return a boolean value.

Following is a simple example demonstrating how to use that method:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
        function saveChanges() {
            var grid = $find("<%=RadGrid1.ClientID%>");
            var masterTable = grid.get_masterTableView();
            var batchManager = grid.get_batchEditingManager();
            var hasChanges = batchManager.hasChanges(masterTable);
            if (hasChanges) {
                batchManager.saveChanges(masterTable);
            }
        }
    </script>
</telerik:RadCodeBlock>
  
<telerik:RadButton runat="server" ID="RadButton1" AutoPostBack="false" Text="Save changes" OnClientClicked="saveChanges"></telerik:RadButton>
  
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnBatchEditCommand="RadGrid1_BatchEditCommand">
    <MasterTableView EditMode="Batch" CommandItemDisplay="Top">
    </MasterTableView>
</telerik:RadGrid>

And the dummy data:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("BoolValue", typeof(Boolean));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
    }
  
    (sender as RadGrid).DataSource = table;
}
  
protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{
    foreach (GridBatchEditingCommand command in e.Commands)
    {
          
    }
}


Best Regards,
Konstantin Dikov
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.

 
0
Kavitha
Top achievements
Rank 1
answered on 07 Nov 2014, 10:03 AM
Thank you
0
Tim Inouye
Top achievements
Rank 1
answered on 23 May 2015, 07:24 PM

Konstantin,

 I have applied this change and am still unable to have newly created rows passed to the BatchEditCommand event.

I am dynamically generating the Radgrid from code.

 I am using the trial version of Telerik 1st quarter 2015. We are in the process of acquiring the licensed version. I'm thinking the license would not cause this behavior.

Here are snippets of my code:

    <form id="frmList" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function saveChanges() {
                var grid = $find("<%=grdList.ClientID%>");
                var masterTable = grid.get_masterTableView();
                var batchManager = grid.get_batchEditingManager();
                var hasChanges = batchManager.hasChanges(masterTable);
                if (hasChanges) {
                    batchManager.saveChanges(masterTable);
                }
            }
        </script>
    </telerik:RadCodeBlock>
     <telerik:RadButton ID="btnSave" AutoPostBack="false" OnClientClicked="saveChanges" runat="server" Text=" Save "></telerik:RadButton>
        <br /><br />
    <telerik:RadGrid ID="grdList" OnBatchEditCommand="grdList_BatchEditCommand" AllowSorting="True" AllowPaging="True" AllowAutomaticUpdates="true" AllowAutomaticInserts="True"
             AutoGenerateColumns="False" ShowStatusBar="true" runat="server">
     <MasterTableView  EditMode="Batch" CommandItemDisplay="Top" DataKeyNames="SalesCode" Name="SalesCodes"> 
        <BatchEditingSettings OpenEditingEvent="Click" EditType="Row" />
        <CommandItemSettings ShowSaveChangesButton="false" CancelChangesText="Cancel"/>
     </MasterTableView>
    </telerik:RadGrid>
    </div>
    </form>

______________________________________________

                            colText = New GridBoundColumn
                            colText.HeaderText = rsData.Item("FieldCaption")
                            colText.DataField = rsData.Item("FieldName")
                            colText.MaxLength = rsData.Item("FieldLength")
                            iFormWidth = iFormWidth + rsData.Item("FieldWidth") + 3
                            If Not IsDBNull(rsData.Item("FieldDefault")) Then
                                structDefaultValues(iDefaultValues).iCellNumber = rsData.Item("FieldID")
                                structDefaultValues(iDefaultValues).sDefault = rsData.Item("FieldDefault")
                                structDefaultValues(iDefaultValues).sType = rsData.Item("FieldType")
                                iDefaultValues = iDefaultValues + 1
                            End If
                            grdList.MasterTableView.EditFormSettings.CaptionDataField = sKeyField
                            grdList.MasterTableView.Name = strName
                            grdList.MasterTableView.DataKeyNames = arrKeys
                            grdList.Columns.Add(colText)

 

_____________________________________________________

            '--- load fields grid
            cmdList = New OleDb.OleDbCommand(sSQLSelect, gdbMtrk)
            daList = New OleDbDataAdapter(sSQLSelect, gdbMtrk)
            daList.SelectCommand = cmdList
            dsList = New DataSet
            cbList = New OleDbCommandBuilder(daList)
            daList.UpdateCommand = cbList.GetUpdateCommand
            daList.InsertCommand = cbList.GetInsertCommand
            daList.DeleteCommand = cbList.GetDeleteCommand
            dsList.Clear()
            daList.Fill(dsList)

            grdList.DataSource = dsList.Tables(0)

 

 _________________________________________________

     Protected Sub grdList_BatchEditCommand(sender As Object, e As GridBatchEditingEventArgs)
        For Each Cmd As GridBatchEditingCommand In e.Commands
            Dim newval As Hashtable = Cmd.NewValues
            Dim oldval As Hashtable = Cmd.OldValues
            'Add to designated table and refresh grid !!!!!
        Next

End Sub

0
Konstantin Dikov
Telerik team
answered on 28 May 2015, 07:07 AM
Hello Tim,

It would be difficult to guess what could cause the issue on your end. However, I have noticed that you are setting AllowAutomaticUpdates/Inserts properties to true, but this will work only when the grid is bound to a DataSource control through the DataSourceID property (as documented in our help article "Automatic DataSource Operations").

Furthermore, can you please elaborate whether you are creating your grid within the Page's Load or Init event handler. I could also suggest that you open a regular support ticket and provide the entire markup and code-behind of your page, so we can have a better idea of your exact implementation.


Best Regards,
Konstantin Dikov
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
Tags
Grid
Asked by
Kavitha
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Kavitha
Top achievements
Rank 1
Tim Inouye
Top achievements
Rank 1
Share this question
or