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
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvTest" runat= "server" OnRowDataBound="gvTest_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Delete" >
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" onCommand="gvTest_Delete" CommandName="deleterow"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enter Key">
<ItemTemplate>
<asp:TextBox ID="txtboxTest" Width="90" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select Value">
<ItemTemplate>
<!-- <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>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource id="SqlDS01" runat="server" ConnectionString="Data Source=PALERIDER-PC;Initial Catalog=Login;Integrated Security=True"
SelectCommand = "select stid, name from login" >
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
.cs
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();
try
{
sqlcon.Open();
sqlda.Fill(ds);
gvTest.DataSource = ds;
gvTest.DataBind();
}
catch (Exception exc)
{
Response.Write(exc.ToString());
}
finally
{
sqlcon.Close();
}
}
}
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);
//Response.Write(index_row);
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)
{
}
}