In this article I will explain with an example, how to
delete a row from
temporary DataTable bound to
GridView in
ASP.Net.
HTML Markup
The
HTML Markup consists of:
GridView - For displaying data.
Columns
The
GridView consists of two
BoundField columns and a
CommandField column.
<asp:GridView ID="gvCustomers" CssClass="Grid" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>`
Binding the GridView
Inside the
Page_Load event handler, an object of
DataTable is created.
Then, two columns are added to the
DataTable Columns collection using the
AddRange method.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, the
DataTable value is saved in
ViewState variable.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("Item"),
new DataColumn("Price") });
dt.Rows.Add("Shirt", 450);
dt.Rows.Add("Jeans", 3200);
dt.Rows.Add("Trousers", 1900);
dt.Rows.Add("Tie", 185);
dt.Rows.Add("Cap", 100);
dt.Rows.Add("Hat", 120);
dt.Rows.Add("Scarf", 290);
dt.Rows.Add("Belt", 150);
ViewState["dt"] = dt;
BindGrid();
}
}
protected void BindGrid()
{
gvCustomers.DataSource = ViewState["dt"] as DataTable;
gvCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBackThen
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {
New DataColumn("Item"),
New DataColumn("Price")})
dt.Rows.Add("Shirt", 450)
dt.Rows.Add("Jeans", 3200)
dt.Rows.Add("Trousers", 1900)
dt.Rows.Add("Tie", 185)
dt.Rows.Add("Cap", 100)
dt.Rows.Add("Hat", 120)
dt.Rows.Add("Scarf", 290)
dt.Rows.Add("Belt", 150)
ViewState("dt") = dt
BindGrid()
End If
End Sub
Protected Sub BindGrid()
gvCustomers.DataSource = TryCast(ViewState("dt"), DataTable)
gvCustomers.DataBind()
End Sub
Applying the JavaScript Confirmation Box to the GridView CommandField Delete Button
Inside the
OnRowDataBound event handler, a loop is executed over the
Button controls of the
GridView Cell. If the
CommandName of the
Button is Delete, then
JavaScript Confirmation Box script is assigned to its
OnClick attribute.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
}
}
}
}
VB.Net
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim item As String = e.Row.Cells(0).Text
For Each button As Button In e.Row.Cells(2).Controls.OfType(Of Button)()
If button.CommandName = "Delete" Then
button.Attributes("onclick") = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"
End If
Next
End If
End Sub
Delete the ASP.Net GridView Row using CommandField and OnRowDeleting event
When the
Delete Button is clicked, the
OnRowDeleting event handler is executed. Inside the
OnRowDeleting event handler, the Index of the
GridView Row is determined and it is used to delete the Row from the
DataTable.Finally the
DataTable is saved back to the
ViewState and the
GridView is again populated.
C#
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"]as DataTable;
dt.Rows[index].Delete();
ViewState["dt"] = dt;
BindGrid();
}
VB.Net
Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Dim index As Integer = Convert.ToInt32(e.RowIndex)
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows(index).Delete()
ViewState("dt") = dt
BindGrid()
End Sub
Screenshot
Demo
Downloads