like i am setting a button i gridview which when i click then shoulf turn row into editable mode, i could use Automatic EDIT button of Gridview but i didn't on purpose, it went good when i developed a delete button, +1, but stuck with this edit , it throws error:
The GridView 'gvTest' fired event RowEditing which wasn't handled.
code:
.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)
{
connectToDb();
}
}
public void connectToDb()
{
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+"</br>");
// 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");
// String delete_comm = "delete from login where stid= '"+index_row+"' ";
int pk = Convert.ToInt32(gvTest.DataKeys[index_row].Value);
SqlConnection sqlcon = new SqlConnection(constrng);
SqlCommand sqlComDelte = new SqlCommand("deleteRow", sqlcon);
sqlComDelte.CommandType = CommandType.StoredProcedure;
sqlComDelte.Parameters.Add("@index", SqlDbType.Int).Value = pk ;
Response.Write("PK=" + pk+"</br>");
//sqlComDelte.Parameters["@index"].Value = index_row;
try
{
sqlcon.Open();
int numRowEffected = Convert.ToInt32(sqlComDelte.ExecuteNonQuery());
Response.Write("Num of rows deleted="+numRowEffected+"</br>");
connectToDb();
Response.Write("Loaded again");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
sqlcon.Close();
}
}
}
protected void gvTest_Edit(Object sender, GridViewEditEventArgs e)
{
gvTest.EditIndex = e.NewEditIndex;
Response.Write("Editing...");
connectToDb();
}
protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
.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" DataKeyNames="stid">
<Columns>
<asp:TemplateField HeaderText="Delete" >
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" onCommand="gvTest_Delete" CommandName="deleterow"
CommandArgument='<%# Container.DataItemIndex %>' />
<asp:Button ID= "btnEdit" runat="server" Text="Edit" OnRowEditing="gvTest_Edit" CommandName="edit"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enter Key">
<ItemTemplate>
<asp:TextBox ID="txtboxTest" Text='<%#Eval("Name") %>' Width="90" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtboxTest" Text='<%#Eval("Name") %>' Width="90" runat="server" />
</EditItemTemplate>
</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>