deleting row from gridview manually


I'm trying to delete a row from gridview manually, i mean that i can use SHOWDELETEBUTTON=TRUE, that's easy but i want to delete it via button code, so far i have done this but couldn'y figure out that what to do more


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
     <asp:GridView ID="gvTest" runat= "server" OnRowDataBound="gvTest_RowDataBound">
       <asp:TemplateField HeaderText="Delete" >
         <asp:Button ID="btnDelete" runat="server"  Text="Delete" onCommand="gvTest_Delete"  CommandName="deleterow" 
          CommandArgument='<%# Container.DataItemIndex %>'   />
       <asp:TemplateField HeaderText="Enter Key">
         <asp:TextBox ID="txtboxTest" Width="90" runat="server" />
       <asp:TemplateField HeaderText="Select Value">
         <!-- <asp:Image ID="img1" runat="server" ImageUrl="DSC03148.jpg" Height="10%" Width="5%" /> -->
        <asp:DropDownList ID= "ddListTest" runat="server" DataSourceid="SqlDS01" DataTextField="name" DataValueField="stid" ></asp:DropDownList>
     <asp:SqlDataSource id="SqlDS01"  runat="server" ConnectionString="Data Source=PALERIDER-PC;Initial Catalog=Login;Integrated Security=True"
          SelectCommand = "select stid, name from login" >


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
    public String constrng = "Data Source=PALERIDER-PC;Initial Catalog=Login;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)

            SqlConnection sqlcon = new SqlConnection(constrng);
            String com1 = "select * from login";
            SqlCommand sqlcom = new SqlCommand(com1, sqlcon);
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
            DataSet ds = new DataSet();

                gvTest.DataSource = ds;
            catch (Exception exc)

    protected void gvTest_RowDataBound(Object sender, GridViewRowEventArgs e) 
        if (e.Row.RowType == DataControlRowType.DataRow) 
            DropDownList ddlist = (DropDownList)e.Row.FindControl("ddListTest");
            ddlist.Items.Insert(0, "Select");

    protected void gvTest_Delete(Object sender, CommandEventArgs e) 
        if (e.CommandName == "deleterow")
            int index_row = Convert.ToInt32(e.CommandArgument);
            Button GrdBtn = (Button)gvTest.Rows[index_row].FindControl("btnDelete");
            TextBox GrdTxtBox = (TextBox)gvTest.Rows[index_row].FindControl("txtboxTest");
            DropDownList GrdDlist = (DropDownList)gvTest.Rows[index_row].FindControl("ddListTest");


    protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article