RowAdding / RowAdded events

The RowAdding and RowAdded events fire when a new row has been added from the jqGrid client-side. This can happen by either end-users invoking the "Add New Row" Dialog functionality or by other client-side triggering of row adding.

The RowAdding event is a "before" event - it fires before the action really executes. The RowAdded event is an "after" event and fires after the row has been added. It is up to the developer to perform the necessary actions in the RowAdding event and to update the datasource accordingly - the grid typically cannot automatically add new rows. RowAdding can also be used to perform server-side validation of the data in the new row and reject the add if the data does not validate.

To hook RowAdding event, just select your JQGrid instance in Visual Studio.NET design time, select Properties (or click F4), then in the Properties window switch to Events and double click the RowAdding event. This will generate something similar to this in your ASPX and code behind:

<trirand:JQGrid ID="JQGrid1" runat="server" OnRowAdding="JQGrid1_RowAdding">

protected void JQGrid1_RowAdding(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e) { }

Now you have RowAdding hooked and the JQGrid1_RowAdding event handler will execute when a new row is added. You can now use the event arguments to retrieve the new data submitted for adding using the RowData name-value collection. You can retrieve data using the key from the DataField of the respective column, e.g. e.RowData["CompanyName"], e.RowData["News"], etc. Pay attention to the type of the column - RowData always returns strings, whereas your database is typically strongly typed, so you may need to check the values for validity before updating the database.

Here is sample code for updating the database in the RowAdding event using ADO.NET DataTable. Same principles apply for arbitrary datasource.

protected void JQGrid1_RowAdding(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e) { DataTable dt = GetData(); DataRow row = dt.NewRow(); if (String.IsNullOrEmpty(e.RowData["CompanyName"])) { JQGrid1.ShowErrorMessage("CompanyName should not be null."); return; } row["CustomerID"] = "NEW" + dt.Rows.Count.ToString(); row["CompanyName"] = e.RowData["CompanyName"]; row["Phone"] = e.RowData["Phone"]; row["PostalCode"] = e.RowData["PostalCode"]; row["City"] = e.RowData["City"]; dt.Rows.InsertAt(row, 0); JQGrid1.DataSource = dt; JQGrid1.DataBind(); }



  Last Updated: 11/1/2009 | © Trirand, 2009