c# - How to use DataTable RowDeleting event

DataTable RowDeleting event
DataTableRowDeletingEvent.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";

        DataColumn dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);
        dc1.AllowDBNull = false;
        dc1.Unique = true;

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "BookName";
        dc2.DataType = typeof(string);

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "Author";
        dc3.DataType = typeof(string);

        dt.Columns.AddRange(new DataColumn[] { dc1, dc2, dc3 });

        Label1.Text = "";
        // add a RowDeleting event handler for the DataTable.
        dt.RowDeleting += new DataRowChangeEventHandler(Row_Deleting);

        dt.Rows.Add(new object[] { 1, "Beginning Mac Programming", "Tim Isted" });
        dt.Rows.Add(new object[] { 2, "Learning iPhone Programming", "Alasdair Allan" });
        dt.Rows.Add(new object[] { 3, "Take Control of Syncing Data in Snow Leopard", "Michael E. Cohen" });

        dt.AcceptChanges();
        GridView1.DataSource = dt;
        GridView1.DataBind();

        //this line delete a row from DataTable
        dt.Rows.RemoveAt(0);

        GridView2.DataSource = dt;
        GridView2.DataBind();
    }

    protected void Row_Deleting(object sender, DataRowChangeEventArgs e)
    {
        Label1.Text += "Action: " + e.Action.ToString() + "<br />";
        Label1.Text += "<i>Row original values.......</i><br />";
        Label1.Text += "Book ID: " + e.Row["BookID", DataRowVersion.Original] + "<br />";
        Label1.Text += "Book Name: " + e.Row["BookName", DataRowVersion.Original] + "<br />";
        Label1.Text += "Author: " + e.Row["Author", DataRowVersion.Original] + "<br />";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DataTable RowDeleting event in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to use DataTable RowDeleting event in ado.net
        </h2>
        <hr width="525" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="525"
            >
            <HeaderStyle BackColor="RoyalBlue" />
            <RowStyle BackColor="OrangeRed" />
            <AlternatingRowStyle BackColor="Orange" />
        </asp:GridView>
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Large"
             ForeColor="DeepPink"
             >
        </asp:Label>
        <br />
        <asp:GridView 
            ID="GridView2"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="525"
            >
            <HeaderStyle BackColor="RoyalBlue" />
            <RowStyle BackColor="OrangeRed" />
            <AlternatingRowStyle BackColor="Orange" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples