





confirm_delete. This simply pops up a confirmation dialog asking the user if he is sure he wants to delete the record. If OK is clicked, the delete happens. If Cancel is clicked, nothing happens. The second thing to note is that our first BoundColumn is an invisible column containing the ProductID (we are using the Northwind Products table), which is the primary key we will use for the delete. Most importantly, please note that we have added a Template Column in which we have placed an asp:Button (you could use a LinkButton instead if you prefer). We have given it an ID of "btnDelete" and a CommandName of "Delete". The latter is what makes it work with the Datagrid's OnDeleteCommand.
<html>
<head>
<title>ConfirmDelDG</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function confirm_delete()
{
if (confirm("Are you sure you want to delete this item?")==true)
return true;
else
return false;
}
</script>
</head>
<body>
<form method="post" runat="server" ID="Form1"><br><br>
<asp:DataGrid id="dtgProducts" runat="server"
CellPadding="6" AutoGenerateColumns="False"
OnDeleteCommand="Delete_Row" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px"
BackColor="White" GridLines="Vertical">
<AlternatingItemStyle BackColor="#DCDCDC" />
<ItemStyle ForeColor="Black" BackColor="#EEEEEE" />
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084" />
<Columns>
<asp:BoundColumn Visible="False" DataField="ProductID" ReadOnly="True" />
<asp:BoundColumn DataField="ProductName" ReadOnly="True" HeaderText="Name" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="Price" DataFormatString="{0:c}"
ItemStyle-HorizontalAlign="Right" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Public Class ConfirmDelDG
Inherits System.Web.UI.Page
Protected WithEvents dtgProducts As System.Web.UI.WebControls.DataGrid
Private strConnection As String = ConfigurationSettings.AppSettings("NorthwindConnection")
Private strSql As String = "SELECT ProductID, ProductName, UnitPrice " _
& "FROM Products WHERE CategoryID = 1"
Private objConn As SqlConnection
Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindTheGrid()
End If
End Sub
Private Sub BindTheGrid()
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "Products")
Disconnect()
dtgProducts.DataSource = ds.Tables("Products")
dtgProducts.DataBind()
End Sub
If objConn Is Nothing Then
objConn = New SqlConnection(strConnection)
End If
If objConn.State = ConnectionState.Closed Then
objConn.Open()
End If
End Sub
Private Sub Disconnect()
objConn.Dispose()
End Sub
ByVal e As DataGridItemEventArgs) Handles dtgProducts.ItemDataBound
Dim btn As Button
If e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem Then
btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)
btn.Attributes.Add("onclick", "return confirm_delete();")
End If
End Sub
' Retrieve the ID of the product to be deleted
Dim ProductID As system.Int32 = System.Convert.ToInt32(E.Item.Cells(0).Text)
dtgProducts.EditItemIndex = -1
' Create and load a DataSet
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "Products")
Disconnect()
' Mark the product as Deleted in the DataSet
Dim tbl As DataTable = ds.Tables("Products")
tbl.PrimaryKey = New DataColumn() _
{ _
tbl.Columns("ProductID") _
}
Dim row As DataRow = tbl.Rows.Find(ProductID)
row.Delete()
' Reconnect the DataSet and delete the row from the database
'-----------------------------------------------------------
' Following section commented out for demonstration purposes
'Dim cb As New SqlCommandBuilder(adapter)
'Connect()
'adapter.Update(ds, "Products")
'Disconnect()
'-----------------------------------------------------------
' Display remaining rows in the DataGrid
dtgProducts.DataSource = ds.Tables("Products")
dtgProducts.DataBind()
End Sub
End Class
You may download the code here.