【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码
2021-07-02 18:04
标签:can gen table mouse idg names orm 数据 new 代码都是基础操作,后续功能还会更新,如有问题欢迎提出和提问....... 前台代码: 后台代码: 运行截图: 【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码 标签:can gen table mouse idg names orm 数据 new 原文地址:http://www.cnblogs.com/888888CN/p/7127354.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ado.netDemo1
{
public partial class Test1 : System.Web.UI.Page
{
SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
//string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
//SqlConnection conn = new SqlConnection(connStr);
//conn.Open();
//string sql = "select * from tb_Students";
//SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
//DataSet dataset = new DataSet();
//adapter.Fill(dataset);
//conn.Close();
//GridView1.DataSource = dataset.Tables[0].ToString();
//GridView1.DataBind();
if(!IsPostBack)
{
shuaxin();
}
}
//刷新数据
private void shuaxin()
{
connStr.Open();
string sql = "select * from tb_Students";
SqlDataAdapter da = new SqlDataAdapter(sql, connStr);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
GridView1.DataKeyNames = new string[] { "SID" };
connStr.Close();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
shuaxin();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
shuaxin();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//( Name, Password, Sex, Age, Address, Phone, QQ)
string sql = "update tb_Students set Name=‘"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() +"‘,Sex=‘"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "‘,Age=‘"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "‘,Address=‘"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "‘,Phone=‘"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "‘,QQ=‘"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() +"‘where SID=‘"
+ GridView1.DataKeys[e.RowIndex].Value.ToString().Trim()+ "‘";
connStr.Open();
SqlCommand cmd = new SqlCommand(sql,connStr);
int EXQ = cmd.ExecuteNonQuery();
if (EXQ == 1)
{
Response.Write("");
}
else
{
Response.Write("");
}
connStr.Close();
GridView1.EditIndex = -1;
shuaxin();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sql = "delete from tb_Students where SID=‘"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"‘";
connStr.Open();
SqlCommand cmd = new SqlCommand(sql,connStr);
int EXQ = cmd.ExecuteNonQuery();
if (EXQ == 1)
{
Response.Write("");
}
else
{
Response.Write("");
}
connStr.Close();
shuaxin();
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
shuaxin();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
//编号
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//编号以及鼠标经过的背景更改
if (e.Row.RowType == DataControlRowType.DataRow)
{
////鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=‘#95CACA‘");
////鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=‘#F0F0F0‘");
////当有编辑列时,避免出错,要加的RowState判断
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm(‘你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?‘)");
//}
}
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
}
}
文章标题:【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码
文章链接:http://soscw.com/essay/100898.html