How to add and remove a grid row from server side

  1. #1

    How to add and remove a grid row from server side

    I looked through the GridPanel's Webservice Connection Examples but my scenario is a little different. All the Examples are modifying the grid on the clientside and then do a final commit with the Save button. However, I want each Add/Delete to hit the server and modify a list stored in session.

    For example, when someone adds a row to the grid, the Example is adding a blank row and allowing the user to modify it. In my scenario, on Add button click, I need to hit server with AjaxEvent, create a row/record with default values, and add the new record to the grid. When someone deletes a row, I need to hit server, determine if row can be deleted, and delete the row from in memory list, delete the row from grid. The way I am handling both right now is by rebinding the grid at the end of Add/Delete. It's not very optimal with large datasets. Is there a better alternative?
  2. #2

    RE: How to add and remove a grid row from server side

    Hi jchau,

    I wrote a small sample which demonstrate how to delete a row as you want (I hope that I right understood your requirements )

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Collections.Generic"%>
    
    <%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest)
            {
                object[] data = new object[]
                    {
                        new object[] {"3m Co", 71.72, 0.02, 0.03, "9/1 12:00am"},
                        new object[] {"Alcoa Inc", 29.01, 0.42, 1.47, "9/1 12:00am"},
                        new object[] {"Altria Group Inc", 83.81, 0.28, 0.34, "9/1 12:00am"},
                        new object[] {"Wal-Mart Stores, Inc.", 45.45, 0.73, 1.63, "9/1 12:00am"}
                    };
    
                this.Store1.DataSource = data;
                this.Store1.DataBind();
            }
        }
        
        protected void DeleteRows(object sender, AjaxEventArgs e)
        {
            //You can set these params if deleted was failed
            //e.Success
            //e.ErrorMessage
            GridPanel1.AddScript("alert('The company \"{0}\" has been deleted');", e.UserParams["SelectedCompany"]);
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Coolite Example - Simple Array Grid</title>
        <link href="../../Css/examples.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
        
        <ext:ScriptManager ID="ScriptManager1" runat="server" StateProvider="None" />
        
        <ext:Store ID="Store1" runat="server">
            <Reader>
                <ext:ArrayReader ReaderID="company">
                    <Fields>
                        <ext:RecordField Name="company" />
                        <ext:RecordField Name="price" Type="Float" />
                        <ext:RecordField Name="change" Type="Float" />
                        <ext:RecordField Name="pctChange" Type="Float" />
                        <ext:RecordField Name="lastChange" Type="Date" DateFormat="n/j h:ia" />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
        </ext:Store>   
        
        <ext:GridPanel 
            ID="GridPanel1" 
            runat="server" 
            StoreID="Store1"
            StripeRows="true"
            Title="Array Grid"
            AutoExpandColumn="Company"
            Width="600" 
            Height="350">
            <ColumnModel ID="ColumnModel1" runat="server">
                <Columns>
                    <ext:Column ColumnId="Company" Header="Company" Width="160" Sortable="true" DataIndex="company" />
                    <ext:Column Header="Price" Width="75" Sortable="true" DataIndex="price"/>
                    <ext:Column Header="Change" Width="75" Sortable="true" DataIndex="change"/>
                    <ext:Column Header="Change" Width="75" Sortable="true" DataIndex="pctChange"/>
                    <ext:Column Header="Last Updated" Width="85" Sortable="true" DataIndex="lastChange"/>
                </Columns>
            </ColumnModel>
            <SelectionModel>
                <ext:RowSelectionModel ID="SelectedRowModel1" runat="server" SingleSelect="true"></ext:RowSelectionModel>
            </SelectionModel>
            <Buttons>
                <ext:Button ID="DeleteButton" runat="server" Text="Delete">
                    <AjaxEvents>
                        <Click OnEvent="DeleteRows" 
                            Success="#{GridPanel1}.deleteSelected();"
                            Before="return #{GridPanel1}.hasSelection();">
                            <UserParams>
                                <ext:Parameter Name="SelectedCompany" Value="#{GridPanel1}.selModel.getSelected().id" Mode="Raw" />
                            </UserParams>
                            <EventMask ShowMask="true" Target="CustomTarget" CustomTarget="#{GridPanel1}.body" />
                        </Click>
                    </AjaxEvents>
                </ext:Button>
            </Buttons>
        </ext:GridPanel>
        
        
        </form>
        
       </body>
    </html>
    You can use the same technic for adding a row

    Vladimir Shcheglov
    Sr. Developer
  3. #3

    RE: How to add and remove a grid row from server side

    Thanks vladimir! That delete method makes perfect sense. However, I dont think that works for the Add because I have to modify the store during the AjaxEvent. A new record has to be created on the server side with some values and then appended to the store.
  4. #4

    RE: How to add and remove a grid row from server side

    Any thoughts on adding rows to a GridPanel during an AjaxEvent without rebinding the whole row?

Similar Threads

  1. [CLOSED] Create Editable Grid from server side
    By Marcelo in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 15, 2011, 1:31 PM
  2. get selected row in grid and pass to server side
    By Aleksa007 in forum 1.x Help
    Replies: 0
    Last Post: Mar 07, 2011, 11:58 PM
  3. Update data and refreshing grid on server-side !
    By Aleksa007 in forum 1.x Help
    Replies: 0
    Last Post: Mar 03, 2011, 12:25 AM
  4. Clear Grid data server side
    By bsnezw in forum 1.x Help
    Replies: 2
    Last Post: Oct 26, 2009, 11:48 AM
  5. Update grid from Server Side
    By Maia in forum 1.x Help
    Replies: 3
    Last Post: Jun 03, 2009, 2:21 PM

Posting Permissions