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

[Solved] batch update with insert

2 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
TriZetto
Top achievements
Rank 1
TriZetto asked on 28 Jul 2009, 09:06 PM
I am developing from the demo you provided here: http://demos.telerik.com/aspnet-ajax/calendar/examples/datepicker/shareddatepicker/defaultcs.aspx?product=grid

In the OnItemCommand event handler I am looping through all the fields that have been changed and save or delete them when the "Update4 All" button is clicked. The problem is when the user clicks the "Add new Record" button. In the UI, the row appears and the user can add new values for each column. But when they click "Update All" this new row is not in the RadGrid1.Items collection. I would like to add this while I am updating and deleting the other rows. Is there a way to do this?

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) 
    if (e.CommandName == "UpdateChanges"
    { 
        ArrayList editedItems = new ArrayList(); 
        foreach (GridDataItem item in RadGrid1.Items) 
        { 
            if ((item is GridEditableItem) && (item.IsInEditMode)) 
            { 
                Hashtable ht = new Hashtable(); 
 
 
                //add in delete flag 
                ht["ShouldDelete"] = false
                if (item.Selected) 
                { 
                    ht["ShouldDelete"] = true
                } 
                //add in read only columns 
                ht["ServerID"] = (item.EditManager.GetColumnEditor("ServerID"as GridTextBoxColumnEditor).TextBoxControl.Text; 
 
                if (item.Selected || presenter.ValuesChanged((string)ht["ServerID"])) 
                { 
                    item.ExtractValues(ht); 
 
                    //add in any template columns 
                    ht["OrderDate"] = ((TextBox)(item.EditManager.GetColumnEditor("OrderDate"as GridTemplateColumnEditor).ContainerControl.FindControl("TextBox1")).Text; 
 
                    editedItems.Add(ht); 
                } 
            } 
        } 
 
        if (editedItems.Count > 0) 
        { 
            presenter.UpdateAll(editedItems); 
            XmlDataSource1.Save(); 
            RadGrid1.Rebind(); 
            notificationDisplayMsg.NotificationType = Trizetto.Web.UI.Controls.NotificationType.Information; 
            notificationDisplayMsg.Text = "All changes have been saved."
        } 
    } 



2 Answers, 1 is accepted

Sort by
0
TriZetto
Top achievements
Rank 1
answered on 28 Jul 2009, 09:30 PM
below is the grid definition:
<telerik:RadGrid ID="RadGrid1" runat="server"  
                EnableEmbeddedSkins="false"  
                Skin="WebBlue"  
                EnableEmbeddedBaseStylesheet="false"  
                ImagesPath="~/Css/WebBlue/Grid" 
                EnableLinqExpressions="false" 
                width="100%" 
                AllowMultiRowSelection="true" 
                AutoGenerateColumns="False" DataSourceID="XmlDataSource1" 
                OnItemCreated="RadGrid1_ItemCreated" 
                OnItemCommand="RadGrid1_ItemCommand" 
                > 
                <ClientSettings EnableRowHoverStyle="true"
                    <Selecting AllowRowSelect="true" /> 
                    <Scrolling AllowScroll="true" UseStaticHeaders="true" /> 
                </ClientSettings> 
                <MasterTableView AllowAutomaticInserts="True" CanRetrieveAllData="true"  DataKeyNames="ServerID" EditMode="InPlace" CommandItemDisplay="TopAndBottom" AllowPaging="false"
                    <CommandItemTemplate> 
                        <div id="leftCmd" style="float:left;padding: 5px 5px;"
                            <asp:LinkButton ID="LinkButton2" runat="server" CommandName="InitInsert"  Visible='True'><img style="border:0px;vertical-align:middle;" alt="" src="<%= Page.ResolveUrl("~/css/WebBlue/Grid/AddRecord.gif")%>/>Add new record</asp:LinkButton>&nbsp;&nbsp; 
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="UpdateChanges"  Visible='True'><img style="border:0px;vertical-align:middle;" alt="" src="<%= Page.ResolveUrl("~/css/WebBlue/Grid/AddRecord.gif")%>/>Update All</asp:LinkButton>&nbsp;&nbsp; 
                        </div> 
                        <div id="rightCmd" style="float:right;padding:5px 5px;"
                             
                        </div> 
                    </CommandItemTemplate> 
                    <Columns> 
                        <telerik:GridClientSelectColumn  UniqueName="ClientSelectColumn" HeaderText="Mark for deletion" > 
                            <HeaderStyle Width="35px" /> 
                        </telerik:GridClientSelectColumn> 
                        <telerik:GridBoundColumn  UniqueName="ServerID" DataField="ServerID" HeaderText="ServerID" SortExpression="ServerID" 
                            ReadOnly="true" /> 
                        <telerik:GridNumericColumn  UniqueName="PartNum" DataField="PartNum" HeaderText="Part number" SortExpression="PartNum" 
                            DataFormatString="{0:C}" /> 
                        <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Name" SortExpression="Name" /> 
                        <telerik:GridBoundColumn UniqueName="Group" DataField="Group" HeaderText="Group" SortExpression="Group" /> 
                        <telerik:GridTemplateColumn DataField="OrderDate" UniqueName="OrderDate" HeaderText="Order Date" SortExpression="OrderDate"  > 
                            <EditItemTemplate> 
                                <asp:TextBox ID="TextBox1"  CssClass="radEnabledCss_Default" Text='<%# Eval("OrderDate", "{0:d}") %>' 
                                    onclick="showPopup(this, event);" onfocus="showPopup(this, event);" onblur="parseDate(this, event)" 
                                    runat="server"></asp:TextBox> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 

0
Sebastian
Telerik team
answered on 29 Jul 2009, 06:37 AM
Hello TriZetto,

To reference the insert form item when performing batch updates, consider using the MasterTableView's GetInsertItem() method. Sample usage of it can be found in the following topic from the online documentation.

Keep in mind that this method is meaninful only when the insertion form is displayed in the grid.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
TriZetto
Top achievements
Rank 1
Answers by
TriZetto
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or