gridview not showing new updated values when rowupdating event is triggered - asp.net

am using a gridview. When I click on the edit button the update and cancel button appear. Upon modifying the values in textbox which come from EditItemTemplate , the new values dont show in the event handler rowupdating(), instead I get the values which appear when the page was rendered. How do I grab the new values from these textboxes and proceed further? Here is the code.
aspx page
<%# Page Language="C#" MasterPageFile="~/Master Pages/FacultyMasterpage.master" AutoEventWireup="true" CodeFile="FacultyQuestion.aspx.cs" Inherits="Faculty_FacultyQuestion" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content10" Runat="Server">
<br />
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
AllowPaging="True" AutoGenerateEditButton="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<RowStyle BackColor="White" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
<br />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content20" Runat="Server">
</asp:Content>
cs file coading
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Faculty_FacultyQuestion : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
private void BindData()
{
DataTable dt = Class3.QuestionForFaculty(Convert.ToInt32(Session["rid"]));
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
{
//Retrieve the table from the session object.
DataTable dt = Class3.QuestionForFaculty(Convert.ToInt32(Session["rid"]));
GridView1.DataSource = dt;
GridView1.DataBind();
//Update the values.
GridViewRow row = GridView1.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["ans"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
GridView1.EditIndex = -1;
BindData();
}
}
}
class 3 coading
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.ComponentModel;
[DataObject]
/// <summary>
/// Summary description for Class3
/// </summary>
public class Class3
{
private static string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
[DataObjectMethod(DataObjectMethodType.Select)]
public static DataTable getall()
{
SqlDataAdapter dp = new SqlDataAdapter("Select * from Question", conn);
DataSet ds = new DataSet();
dp.Fill(ds, "Question");
return ds.Tables["Question"];
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public static int delete(int qid)
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("Delete into Question qid = #a", con);
cmd.Parameters.Add("#a", SqlDbType.VarChar).Value = qid;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return(i);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public static DataTable QuestionForFaculty(int rid)
{
SqlDataAdapter dp = new SqlDataAdapter("SELECT Question.question, Question.ans,Question.qid, Subject.subjectname, Question.posteddate, Registration.name, Registration.role, Registration.rid FROM Question INNER JOIN Registration ON Question.rid = Registration.rid INNER JOIN Subject ON Question.sid = Subject.subjectid WHERE (Question.fid = '"+rid+"')", conn);
DataSet ds = new DataSet();
dp.Fill(ds, "Question");
return ds.Tables["Question"];
}
}

Why are you databinding the GridView at the start of the OnRowUpdating event? That right there is your problem! Just remove this:
GridView1.DataSource = dt;
GridView1.DataBind();
UPDATE: first add this to your Class3 (purely based on your sample code - not tested):
[DataObjectMethod(DataObjectMethodType.Update)]
public static int UpdateAnswer(int qid, string ans)
{
var con = new SqlConnection(conn);
var cmd = new SqlCommand("UPDATE Question SET ans = #ans WHERE qid = #qid", con);
cmd.Parameters.Add("#qid", SqlDbType.VarChar).Value = qid;
cmd.Parameters.Add("#ans", SqlDbType.VarChar).Value = ans;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return(i);
}
Then change OnRowUpdating event to:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Class3.UpdateAnswer(row.DataItemIndex, ((TextBox)row.Cells[2].Controls[0]).Text);
GridView1.EditIndex = -1;
BindData();
}

Related

Cannot update selected lines from data in gridview

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services.Description;
namespace Lab06
{
public partial class AssignmentAttendance : System.Web.UI.Page
{
Student sStudent = new Student();
Attendance aAttendance = new Attendance();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
List<Student> acclist = new List<Student>();
acclist = sStudent.getAccountAll();
GridView1.DataSource = acclist;
GridView1.DataBind();
}
protected void Retrieve_Attendance_Click(object sender, EventArgs e)
{
string _connStr = ConfigurationManager.ConnectionStrings["HealthDBContext"].ConnectionString;
SqlConnection SQLConn = new SqlConnection(_connStr);
/* string username = Session["User"].ToString();*/
SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE ClassCode = #ClassCode", SQLConn);
SQLConn.Open();
cmd.Parameters.AddWithValue("#ClassCode", tb_classcode.Text);
cmd.ExecuteNonQuery();
SQLConn.Close();
SqlDataAdapter SQLAdapter = new SqlDataAdapter(cmd);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int result = 0;
Student stud = new Student();
string categoryID = GridView1.DataKeys[e.RowIndex].Value.ToString();
result = stud.AccountDelete(categoryID);
if (result > 0)
{
Response.Write("<script>alert('Student Remove successfully');</script>");
}
else
{
Response.Write("<script>alert('Student Removal NOT successfully');</script>");
}
Response.Redirect("AssignmentAttendance.aspx");
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int result = 0;
Student stud = new Student();
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
string tStudent_ID = ((TextBox)row.Cells[0].Controls[0]).Text;
string tClassCode = ((TextBox)row.Cells[3].Controls[0]).Text;
result = stud.AttendanceUpdate(tClassCode, tStudent_ID);
/*if (result > 0)
{
Response.Write("<script>alert('Product updated successfully');</script>");
}
else
{
Response.Write("<script>alert('Product NOT updated');</script>");
}*/
GridView1.EditIndex = -1;
bind();
}
}
}
Above is my AssignmentAttendance.aspx.cs code
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AssignmentAttendance.aspx.cs" Inherits="Lab06.AssignmentAttendance" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<p>
Attendance</p>
<p>
Filter Search</p>
<p>
Class code <asp:TextBox ID="tb_classcode" runat="server"></asp:TextBox>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="168px" Width="1451px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField AccessibleHeaderText="Joe" DataField="Student_ID" HeaderText="Student_ID" />
<asp:BoundField AccessibleHeaderText="Joe" DataField="Name" HeaderText="Name" />
<asp:BoundField AccessibleHeaderText="Joe" DataField="Pass" HeaderText="Pass" />
<asp:BoundField DataField="ClassCode" HeaderText="ClassCode" />
<asp:CommandField AccessibleHeaderText="Joe" ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
</p>
<asp:Button ID="Retrieve_Attendance" runat="server" Text="Confirm" style="float:right;" OnClick="Retrieve_Attendance_Click" />
</asp:Content>
Above is my AssignmentAttendance.aspx codes
My normal retrieve works however, when I click on the update button, it will not update the current data it has in the gridview but in fact retrieve all the data from the db and display it into the grid view.
My normal retrieve
My gridview after pressing update
Anyone know why this happens?

How to save and retrieve image from database in ASP.NET

I want to show image in image control on page load. I'm using the following code. My problem is that the image is saved in the database as binary data, but I can't retrieve the image in the image control
public partial class TeacherProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
profilepic();
}
protected void upload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
HttpPostedFile file = FileUpload1.PostedFile;
Byte[] imgbyte = new Byte[file.ContentLength];
file.InputStream.Read(imgbyte, 0, file.ContentLength);
SqlCommand cmd = new SqlCommand("Update Registration set Photo = '" + imgbyte + "' where id ='" + idd.Text + "' ", con);
//cmd.Parameters.AddWithValue("#userid", 222); //image id
//cmd.Parameters.AddWithValue("#pic", imgbyte);
//try
//{
con.Close();
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "Image Uploaded";
con.Close();
//}
//catch
//{
// Label1.Text = "Try Again";
//}
}
}
public void profilepic()
{
SqlCommand cmd2 = new SqlCommand("select Photo from Registration where Username = '" + username1.Text + "'", con);
//cmd2.Parameters.AddWithValue("#userid", username1.Text);
con.Open();
SqlDataReader dr = cmd2.ExecuteReader();
if (dr.Read())
{
byte[] bytes = (byte[])dr["Photo"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
cmd2.ExecuteNonQuery();
con.Close();
}
else
{
}
}
}
Can anybody help me please?
Thanks in advance...
my code for file upload
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="fileUpload.aspx.cs" Inherits="fetchImage.fileUpload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
margin-left: 0px;
}
.auto-style2 {
margin-left: 6px;
margin-top: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<div>
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto- style1"></asp:TextBox>
<asp:Button ID="Button1" runat="server" CssClass="auto-style2" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
file upload 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;
namespace fetchImage
{
public partial class fileUpload : System.Web.UI.Page
{
string path;
SqlConnection con = new SqlConnection("Data Source=DESKTOP- U0NOKBP\\SQLEXPRESS;Initial Catalog=Lnmi;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Request.PhysicalApplicationPath + "/Images/"+ FileUpload1.FileName.ToString());
path = "Images/"+FileUpload1.FileName.ToString();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Images values('"+path.ToString()+"','"+TextBox1.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
}
}
}
for showing the file
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ShowImage.aspx.cs" Inherits="fetchImage.ShowImage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<img src="<%#Eval("image_path") %>" height="100" width="100" />
<td><%#Eval("title") %></td>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
file show 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;
namespace fetchImage
{
public partial class ShowImage : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP- U0NOKBP\\SQLEXPRESS;Initial Catalog=Lnmi;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Images";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
con.Close();
}
}
}
please create a table before proceeding
create table Images(image_path varchar(MAX), title varchar(50));
and lastly add a folder Images in your project by right clicking at your project name

ASP.NET Gridview Sorting not functioning

i m trying to implement a gridview on a web application, using asp.net. I m encountering a problem when it comes to sorting my datagrid, by a method - and i d like to ask for advice. This is my .aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="GridViewDemo1.Employee" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView
ID="grvEmployee"
runat="server"
AutoGenerateColumns="true"
BackColor="AliceBlue"
ForeColor="Goldenrod"
BorderColor="YellowGreen"
BorderStyle="Groove"
Width="70%"
CellPadding="3"
CellSpacing="2"
AllowSorting="True"
OnSorting="GridView1_Sorting"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true" ViewStateMode="Enabled">
<RowStyle
HorizontalAlign="Center">
</RowStyle>
<FooterStyle
ForeColor="#8C4510"
BackColor="#F7DFB5">
</FooterStyle>
<PagerStyle
ForeColor="#8C4510"
HorizontalAlign="Center">
</PagerStyle>
<HeaderStyle
ForeColor="White"
Font-Bold="True"
BackColor="#A55129">
</HeaderStyle>
</asp:GridView>
</div>
</form>
and this is the .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace GridViewDemo1
{
public partial class Employee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
string selectSQL = "SELECT * from dbo.[User]";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Employee");
grvEmployee.DataSource = ds;
grvEmployee.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//dataTable.DefaultView.Sort = e.SortExpression;
//grvEmployee.DataSource = dataTable;
grvEmployee.DataBind();
}
}
}
This is my web.config connectionstring:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=xxxxxx;Initial Catalog=yyyyyyy;User ID=zzzzz;Password=xxxxxx;" providerName="System.Data.SqlClient" />
The gridview gets populated properly, i used to get the "fired event Sorting which wasn't handled." but now i simply get no response from trying to sort a column. Does this even work with auto-generated columns? Where can i specify a sort expression? (Ascending/Descending etc)?
You have to do the initial DataBinding only if !IsPostback not on every consecutive postback:
if(!IsPostBack)
{
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
string selectSQL = "SELECT * from dbo.[User]";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Employee");
grvEmployee.DataSource = ds;
grvEmployee.DataBind();
}
In GridView1_Sorting you have to select the ordered data from the database and assign it to the grid's DataSource property, then call grvEmployee.DataBind():
GridView Sorting Sample:https://stackoverflow.com/a/6602125/284240

GridView lost sorting after paging?

I got the below code from internet. It is working properly. I have added paging also. When I'm just sorting, it is working properly. When I am changing the page index, the sorting is lost.
Here is the client side code that set a gridview with 20 items per page, using the sort linked to the "GridView1_Sorting" method in the server side code.
Client side
<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="False" PageSize="20" AllowPaging="True" AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="4">
<Columns>
<asp:TemplateField HeaderText="Employee no" SortExpression="eno">
<ItemTemplate>
<%#Eval("eno")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp name" SortExpression="empname">
<ItemTemplate>
<%#Eval("empname")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary" SortExpression="sal">
<ItemTemplate>
<%#Eval("sal")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And now the server side code:
Server side
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd;
SqlDataAdapter da;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridData();
}
}
void GridData()
{
sqlcmd = new SqlCommand("select * from emp", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Session["dt"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private string GVSortDirection
{
get { return ViewState["SortDirection"] as string ?? "DESC"; }
set { ViewState["SortDirection"] = value; }
}
private string GetSortDirection()
{
switch (GVSortDirection)
{
case "ASC":
GVSortDirection = "DESC";
break;
//assign new direction as ascending order
case "DESC":
GVSortDirection = "ASC";
break;
}
return GVSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = (DataTable)Session["dt"];
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
string sortDirection = GetSortDirection();
dataView.Sort = e.SortExpression + " " + sortDirection;
GridView1.DataSource = dataView;
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridData();
}
}
When you page, the PageIndexChanging event is called. This in turn runs the GridData() procedure, which sets the data source for the gridview to be a data table containing records from the emp table, with no particular sort order.
What you should do is to take the code that you've written in your GridView1_Sorting event-handler, and include this within the GridData routine, so that whenever the grid is populated with data - whether when the page first loads, when the page index is changed or when the gridview is sorted - the gridview is based on a sorted dataview, rather than an unsorted data table.
The way you are maintaining SortDirection using GetSortDirection, in same fashion maintain SortExpression.
Happy coding!!!
Have a look to this article, may you will get what you want

Why The Gridview "AutoGenerateEditButton = true "property does not work in run time?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class ExptGridviewEdit : System.Web.UI.Page
{
SqlCommand com;
SqlDataAdapter da;
DataTable dtb;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["gj"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
com = new SqlCommand("Select * from tblExpt", con);
da = new SqlDataAdapter(com);
dtb = new DataTable();
da.Fill(dtb);
if (dtb.Rows[0] != null)
{
BindData();
}
GridView1.AutoGenerateEditButton = true;
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
GridView1.DataKeyNames = new string[] { "id" };
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
// }
}
protected void BindData()
{
GridView1.DataSource = dtb;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = dtb;
GridView1.DataBind();
}
}
When a data source control that supports updating is bound to a GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic updating functionality.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogenerateeditbutton.aspx

Resources