📄 datagridedit.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction.数据绑定控件
{
/// <summary>
/// DataGridEdit 的摘要说明。
/// </summary>
public class DataGridEdit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgEdit;
private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
}
private void DataGridDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select top 5 EmployeeID,LastName,FirstName,City from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
dgEdit.DataSource = ds.Tables["testTable"];
dgEdit.DataBind();
}
catch(Exception error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgEdit.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgEdit_CancelCommand);
this.dgEdit.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgEdit_EditCommand);
this.dgEdit.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgEdit_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgEdit_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//设置DataGrid控件的编辑项的索引为-1,既取消编辑
dgEdit.EditItemIndex = -1;
//数据绑定
DataGridDataBind();
}
private void dgEdit_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//设置DataGrid控件的编辑项的索引为选择的当前项
dgEdit.EditItemIndex = e.Item.ItemIndex;
//数据绑定
DataGridDataBind();
}
private void dgEdit_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得编辑行的关键字段的值
int empID = (int)dgEdit.DataKeys[e.Item.ItemIndex];
//取得文本框中输入的内容
TextBox newCity = (TextBox)e.Item.FindControl("txtCity");
//定义SQL语句
string sqlCom = "update Employees set City='" + newCity.Text + "' where EmployeeID="+empID.ToString();
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
//打开数据连接
conn.Open();
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
dgEdit.EditItemIndex = -1;
DataGridDataBind();
}
catch(Exception err)
{
//输出异常信息
Response.Write(err.ToString());
}
finally
{
//关闭连接对象
conn.Close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -