View Full Version : How to add and remove a grid row from server side

Oct 12, 2008, 4:58 AM
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?

Oct 12, 2008, 6:27 AM
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"

<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;

protected void DeleteRows(object sender, AjaxEventArgs e)
//You can set these params if deleted was failed
GridPanel1.AddScript("alert('The company \"{0}\" has been deleted');", e.UserParams["SelectedCompany"]);

<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" />
<form id="form1" runat="server">

<ext:ScriptManager ID="ScriptManager1" runat="server" StateProvider="None" />

<ext:Store ID="Store1" runat="server">
<ext:ArrayReader ReaderID="company">
<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" />

Title="Array Grid"
<ColumnModel ID="ColumnModel1" runat="server">
<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"/>
<ext:RowSelectionModel ID="SelectedRowModel1" runat="server" SingleSelect="true"></ext:RowSelectionModel>
<ext:Button ID="DeleteButton" runat="server" Text="Delete">
<Click OnEvent="DeleteRows"
Before="return #{GridPanel1}.hasSelection();">
<ext:Parameter Name="SelectedCompany" Value="#{GridPanel1}.selModel.getSelected().id" Mode="Raw" />
<EventMask ShowMask="true" Target="CustomTarget" CustomTarget="#{GridPanel1}.body" />



You can use the same technic for adding a row

Oct 12, 2008, 2:59 PM
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.

Oct 24, 2008, 2:05 PM
Any thoughts on adding rows to a GridPanel during an AjaxEvent without rebinding the whole row?