How to confirm delete in DataGrid using PushButton

<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=’False’ OnDeleteCommand =’DelCmd’ OnItemCreated =’ItemCrt’ DataKeyField=’Employeeid’ runat=’server’> <Columns> <asp:ButtonColumn Text=’Delete’ ButtonType=’PushButton’ CommandName=’Delete’></asp:ButtonColumn> <asp:BoundColumn DataField=’firstname’ HeaderText=’First Name’></asp:BoundColumn> </Columns> </asp:DataGrid> VB.NET Dim sqlStmt As String Dim conString As String Dim cn As SqlConnection = Nothing Dim da As SqlDataAdapter = Nothing Dim ds As DataSet Private Sub Page_Load(sender As Object, e As System.EventArgs) conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’ cn = New SqlConnection(conString) If Not Page.IsPostBack Then BindData() End If End Sub ’Page_Load Sub BindData() sqlStmt = ‘select * from emp ‘ ds = New DataSet() da = New SqlDataAdapter(sqlStmt, cn) da.Fill(ds, ‘t1’) DataGrid1.DataSource = ds DataGrid1.DataBind() End Sub ’BindData Protected Sub ItemCrt(sender As Object, e As DataGridItemEventArgs) Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem Dim btn As Button = CType(e.Item.Cells(0).Controls(0), Button) btn.Attributes.Add(‘onclick’, ‘return confirm(’are you sure you want to delete this’)’) Exit End Select End Sub ’ItemCrt Protected Sub DelCmd(sender As [Object], e As DataGridCommandEventArgs) DeleteRow(Me.DataGrid1.DataKeys(e.Item.ItemIndex).ToString()) BindData() End Sub ’DelCmd Private Sub DeleteRow(empid As String) Dim cmd As New SqlCommand(‘DELETE FROM Emp WHERE employeeid =’ + empid, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close() End Sub ’DeleteRow C# string sqlStmt ; string conString ; SqlConnection cn =null; SqlDataAdapter da =null; DataSet ds; private void Page_Load(object sender, System.EventArgs e) { conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’; cn = new SqlConnection(conString); if (!Page.IsPostBack ) { BindData(); } } void BindData() { sqlStmt = ‘select * from emp ‘; ds= new DataSet (); da = new SqlDataAdapter (sqlStmt, cn); da.Fill (ds,’t1’); DataGrid1.DataSource =ds; DataGrid1.DataBind (); } protected void ItemCrt(object sender, DataGridItemEventArgs e) { switch(e.Item.ItemType) { case ListItemType.Item: case ListItemType.AlternatingItem: { Button btn = (Button)e.Item.Cells[0].Controls[0]; btn.Attributes.Add(‘onclick’, ‘return confirm(’are you sure you want to delete this’)’); break; } } } protected void DelCmd(Object sender , DataGridCommandEventArgs e ) { DeleteRow (this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()); BindData(); } private void DeleteRow(string empid) { SqlCommand cmd = new SqlCommand(‘DELETE FROM Emp WHERE employeeid =’+ empid ,cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }

How to confirm delete in DataGrid using LinkButton

<script> function confirmmsg() { if (confirm(‘Do you want to delete record?’)==true) return true; else return false; } </script> <asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=’False’ DataKeyField=’Employeeid’ OnItemCommand=’ItemCmd’ OnItemDataBound=’ItemDB’ runat=’server’> <Columns> <asp:BoundColumn DataField=’firstname’ HeaderText=’First Name’></asp:BoundColumn> <asp:TemplateColumn> <ItemTemplate> <asp:LinkButton id=’btnDelete’ runat=’server’ Text=’Delete’ CommandName=’Delete’ CausesValidation=’false’></asp:LinkButton> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> VB.NET Dim sqlStmt As String Dim conString As String Dim cn As SqlConnection = Nothing Dim da As SqlDataAdapter = Nothing Dim ds As DataSet Private Sub Page_Load(sender As Object, e As System.EventArgs) conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’ cn = New SqlConnection(conString) If Not Page.IsPostBack Then BindData() End If End Sub ’Page_Load Sub BindData() sqlStmt = ‘select * from emp ‘ ds = New DataSet() da = New SqlDataAdapter(sqlStmt, cn) da.Fill(ds, ‘t1’) DataGrid1.DataSource = ds DataGrid1.DataBind() End Sub ’BindData Protected Sub ItemDB(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs) Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem Dim btn As LinkButton = CType(e.Item.FindControl(‘btnDelete’), LinkButton) btn.Attributes.Add(‘onclick’, ‘return confirmmsg();’) Exit End Select End Sub ’ItemDB Protected Sub ItemCmd(sender As Object, e As System.Web.UI.WebControls.DataGridCommandEventArgs) If e.CommandName = ‘Delete’ Then Me.DeleteRow(Me.DataGrid1.DataKeys(e.Item.ItemIndex).ToString()) End If BindData() End Sub ’ItemCmd Private Sub DeleteRow(empid As String) Dim cmd As New SqlCommand(‘DELETE FROM Emp WHERE employeeid =’ + empid, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close() End Sub ’DeleteRow C# string sqlStmt ; string conString ; SqlConnection cn =null; SqlDataAdapter da =null; DataSet ds; private void Page_Load(object sender, System.EventArgs e) { conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’; cn = new SqlConnection(conString); if (!Page.IsPostBack ) { BindData(); } } void BindData() { sqlStmt = ‘select * from emp ‘; ds= new DataSet (); da = new SqlDataAdapter (sqlStmt, cn); da.Fill (ds,’t1’); DataGrid1.DataSource =ds; DataGrid1.DataBind (); } protected void ItemDB(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { switch(e.Item.ItemType) { case ListItemType.Item: case ListItemType.AlternatingItem: { LinkButton btn = (LinkButton)e.Item.FindControl(‘btnDelete’); btn.Attributes.Add(‘onclick’, ‘return confirmmsg();’); break; } } } protected void ItemCmd(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) { if(e.CommandName == ‘Delete’) { this.DeleteRow(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()); } BindData(); } private void DeleteRow(string empid) { SqlCommand cmd = new SqlCommand(‘DELETE FROM Emp WHERE employeeid =’+ empid ,cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }

How to do Paging using DataGrid

<asp:DataGrid id=’DataGrid1′ AllowPaging =true PageSize =5 OnPageIndexChanged =’PageData’ runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then Binddata() End If End Sub Protected Sub PageData(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) DataGrid1.CurrentPageIndex = e.NewPageIndex Binddata() End Sub Sub Binddata() ’Populate the DataGrid using DataSet End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(! Page.IsPostBack ) { Binddata(); } } protected void PageData(Object source , System.Web.UI.WebControls.DataGridPageChangedEventArgs e ) { DataGrid1.CurrentPageIndex = e.NewPageIndex; Binddata(); } void Binddata() { //Populate the DataGrid using DataSet }

How to do Simple sorting using DataGrid

<asp:DataGrid id=’DataGrid1′ AllowSorting =True OnSortCommand =’SortData’ runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then BindDataGrid(‘ProductId’) End If End Sub Protected Sub SortData(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) BindDataGrid(e.SortExpression.ToString()) End Sub Sub BindDataGrid(ByVal sortfield As String) ’Fill the Dataset ’….. Dim dv As DataView = ds.Tables(0).DefaultView dv.Sort = sortfield DataGrid1.DataSource = dv DataGrid1.DataBind() End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(! Page.IsPostBack ) { BindDataGrid(‘ProductId’); } } protected void SortData(Object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e ) { BindDataGrid(e.SortExpression.ToString()); } void BindDataGrid(string sortfield) { //Fill the Dataset //….. DataView dv = ds.Tables[0].DefaultView; dv.Sort = sortfield; DataGrid1.DataSource = dv; DataGrid1.DataBind(); }