ExEditGridView.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExEditGridView.aspx.cs" Inherits="ExEditGridView" %>
<!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="GridView1" runat="server" onrowupdated="GridView1_RowUpdated"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Security;
using System.Web.Security;
using System.Configuration;
public partial class ExEditGridView : System.Web.UI.Page
{
SqlConnection connObj = new SqlConnection();
DataSet ds = new DataSet();
SqlDataAdapter objAdapter;
protected void Page_Load(object sender, EventArgs e)
{
connObj.ConnectionString = ConfigurationManager.ConnectionStrings["dotNetTrainingConnectionString"].ToString();
connObj.Open();
if (!IsPostBack)
{
BindData();
FillGrid();
}
}
public void BindData()
{
SqlCommand objCommand = new SqlCommand("Select * from student", connObj);
objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(ds);
connObj.Close();
}
public void FillGrid()
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
// Retrieve the value of the first cell
Response .Write ( "Updated record " + row.Cells[1].Text);
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
FillGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
BindData();
GridViewRow row = GridView1.Rows[e.RowIndex];
DataRow drnew =ds.Tables[0].NewRow ();
drnew[1] = ((TextBox)(row.Cells[3].Controls[0])).Text;
drnew[2] = ((TextBox)(row.Cells[4].Controls[0])).Text;
ds.Tables [0].Rows.Add (drnew );
//Update the values.
ds.Tables[0].TableName = "student";
SqlCommand comm = new SqlCommand("UPDATE student SET FirstName=@FirstName,LastName=@LastName WHERE STUDENT_id="+((TextBox)(row.Cells[2].Controls[0])).Text, connObj);
comm.Parameters.Add("@LastName", SqlDbType.VarChar, 255, "LastName");
comm.Parameters.Add("@FirstName", SqlDbType.VarChar, 255, "FirstName");
objAdapter.InsertCommand = comm;
objAdapter.Update(ds, "student");
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
FillGrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Bind data to the GridView control.
BindData();
FillGrid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow delRow = GridView1.Rows[e.RowIndex];
int ID =int.Parse (delRow.Cells[2].Text);
SqlCommand cmd = new SqlCommand("delete FROM STUDENT where Student_Id =" + ID, connObj);
cmd.ExecuteNonQuery();
BindData();
FillGrid();
}
}
No comments:
Post a Comment