asp.net 中利用GridView实现增加,修改,删除
来源:互联网 发布:北京科来软件 编辑:程序博客网 时间:2024/06/05 17:32
1、创建数据库表
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table1](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Employees] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Position] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Team] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
2、ASPX代码
3、后台C#代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView();
}
}
private string GetConnectionString()
{
//Where MyConsString is the connetion string that was set up in the web config file
//return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
return "Data Source=127.0.0.1;Initial Catalog=testdb;User ID=sa;PassWord=newman2007";
}
private void DeleteRecord(string ID)
{
SqlConnection connection = new SqlConnection(GetConnectionString());
string sqlStatement = "DELETE FROM Table1 WHERE Id = @Id";
try
{
connection.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, connection);
cmd.Parameters.AddWithValue("@Id", ID);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
private void BindGridView()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
try
{
connection.Open();
string sqlStatement = "SELECT * FROM Table1";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridViewEmployee.DataSource = dt;
GridViewEmployee.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
private void AddNewRecord(string employee, string position, string team)
{
SqlConnection connection = new SqlConnection(GetConnectionString());
string sqlStatement = "INSERT INTO Table1" +
"(Employees,Position,Team)" +
"VALUES (@Employees,@Position,@Team)";
try
{
connection.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, connection);
cmd.Parameters.AddWithValue("@Employees", employee);
cmd.Parameters.AddWithValue("@Position", position);
cmd.Parameters.AddWithValue("@Team", team);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert/Update Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Extract the TextBoxes that is located under the footer template
TextBox tbEmployee = (TextBox)GridViewEmployee.FooterRow.Cells[0].FindControl("TextBoxEmployee");
TextBox tbPosition = (TextBox)GridViewEmployee.FooterRow.Cells[1].FindControl("TextBoxPosition");
TextBox tbTeam = (TextBox)GridViewEmployee.FooterRow.Cells[2].FindControl("TextBoxTeam");
if (tbEmployee.Text.Trim() != "" && tbPosition.Text.Trim() != "" && tbTeam.Text.Trim() != "")
{
//call the method for adding new records to database and pass the necessary parameters
AddNewRecord(tbEmployee.Text, tbPosition.Text, tbTeam.Text);
//Re-Bind the GridView to reflect the changes made
BindGridView();
}
}
private void UpdateRecord(string id, string employee, string position, string team)
{
SqlConnection connection = new SqlConnection(GetConnectionString());
string sqlStatement = "UPDATE Table1 " +
"SET Employees = @Employees, Position = @Position, Team = @Team " +
"WHERE Id = @Id";
try
{
connection.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, connection);
cmd.Parameters.AddWithValue("@Employees", employee);
cmd.Parameters.AddWithValue("@Position", position);
cmd.Parameters.AddWithValue("@Team", team);
cmd.Parameters.AddWithValue("@Id", id);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert/Update Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
protected void GridViewEmployee_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewEmployee.EditIndex = e.NewEditIndex; // turn to edit mode
BindGridView(); // Rebind GridView to show the data in edit mode
}
protected void GridViewEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridViewEmployee.EditIndex = -1; //swicth back to default mode
BindGridView(); // Rebind GridView to show the data in default mode
}
protected void GridViewEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Accessing Edited values from the GridView
string id = ((Label)GridViewEmployee.Rows[e.RowIndex].Cells[3].FindControl("LabelID")).Text; //ID
string employee = ((TextBox)GridViewEmployee.Rows[e.RowIndex].Cells[0].FindControl("TextBoxEditEmployee")).Text; //Employee
string position = ((TextBox)GridViewEmployee.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditPosition")).Text; //Position
string team = ((TextBox)GridViewEmployee.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditTeam")).Text; //Team
UpdateRecord(id, employee, position, team); // call update method
GridViewEmployee.EditIndex = -1; //Turn the Grid to read only mode
BindGridView(); // Rebind GridView to reflect changes made
Response.Write("Update Seccessful!");
}
protected void GridViewEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//get the ID of the selected row
string id = ((Label)GridViewEmployee.Rows[e.RowIndex].Cells[3].FindControl("LabelID")).Text;
DeleteRecord(id); //call the method for delete
BindGridView(); // Rebind GridView to reflect changes made
}
- asp.net 中利用GridView实现增加,修改,删除
- ASP.NET中数据库的操作初步----增加、删除、修改
- ASP.NET中数据库的操作初步----增加、删除、修改
- ASP.NET中数据库的操作初步----增加、删除、修改
- ASP.NET中数据库的操作初步----增加、删除、修改
- ASP.NET中数据库的操作初步----增加、删除、修改
- ASP.NET中数据库的操作初步----增加、删除、修改
- ASP.Net增加修改删除数据
- ASP.NET实现GridView删除功能
- Asp.net之使用GridView控件的RowCommand事件实现修改和删除
- gridview删除 asp.net
- Asp.net中C#增加删除查找与修改xml文件元素节点
- ASP.NET—005:GridView增加一行JS实现
- ASP.NET操作XML文件---增加、修改、删除、显示
- ASP.NET 操作Cookie详解 增加,修改,删除
- ASP.NET 操作Cookie详解 增加,修改,删除 .
- ASP.NET XML读取、增加、修改和删除操作
- ASP.NET 操作Cookie详解 增加,修改,删除
- Wince应用程序开机自启动的注册表设置
- 第一篇博客
- TC SRM474 500points
- 软件的架构与模式之经典架构模式简介
- 网络硬盘(简称网盘)
- asp.net 中利用GridView实现增加,修改,删除
- 《JSP2.0 技术手册》读书笔记六-JSP语法与EL
- getdtablesize()函数是干什么的
- javascript线程解释(setTimeout,setInterval你不知道的事)(2)
- W3C Javascript 最新Chm格式下载
- ldconfig
- 列表与元组(List and Tuple)
- php set_exception_handler()
- dreamweaver 的思考