当前位置: 代码迷 >> ASP >> Asp.net 利用GridView来展示数据,并做删除,更新,取消操作
  详细解决方案

Asp.net 利用GridView来展示数据,并做删除,更新,取消操作

热度:65   发布时间:2012-12-22 12:05:06.0
Asp.net 利用GridView来显示数据,并做删除,更新,取消操作。

基本步骤:

1.在页面选择GridView空间

2.通过ADO.NET方法链接数据库。

3.在代码中,根据删除,编辑,取消,更新等按钮,执行相应的指定方法。

代码:

using 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 MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace AspDB
{
??? public partial class _Default : System.Web.UI.Page
??? {
??????? protected void Page_Load(object sender, EventArgs e)
??????? {
??????????? if(!this.IsPostBack)//该段代码表示,当页面第一次加载的时候会绑定一次,当页面点更新的时候,一定要写该行代码,要不然就更新不了。因为点更新按钮后
??????????????????????????????? //没有该行,页面就重新获取原来的值了。
??????????? dbBind();
??????? }

??????? protected void Button1_Click(object sender, EventArgs e)
??????? {
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
??????????? MySqlCommand commend = new MySqlCommand("select * from user", con);
??????????? con.Open();
??????????
??????????? if (con.State == System.Data.ConnectionState.Open)
??????????? {
????????????
??????????????? MySqlDataAdapter msda = new MySqlDataAdapter("select * from user limit 50",con);
??????????????? DataSet ds = new DataSet();
??????????????? msda.Fill(ds);
??????????????? con.Close();
??????????????? this.GridView1.DataSource = ds;
??????????????? this.GridView1.DataKeyNames = new string[] { "ID" };
??????????????? this.GridView1.DataBind();
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("<script>alert('数据库连接没打开。');</script>");
??????????? }
????????
??????? }

//该控件如果点击分页按钮,默认会触发PageIndexChanging方法

??????? protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
??????? {
??????????? this.GridView1.PageIndex = e.NewPageIndex;//通过点击页面的分页数字。传到后台的固定取值方法。
??????????? dbBind();
??????? }
??????? private void dbBind()
??????? {
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
??????????? MySqlCommand commend = new MySqlCommand("select * from user", con);
??????????? con.Open();

??????????? if (con.State == System.Data.ConnectionState.Open)
??????????? {

??????????????? MySqlDataAdapter msda = new MySqlDataAdapter("select * from user limit 50", con);
??????????????? DataSet ds = new DataSet();
??????????????? msda.Fill(ds);
??????????????? con.Close();
??????????????? this.GridView1.DataSource = ds;
??????????????? this.GridView1.DataKeyNames = new string[] { "ID"};//该段代码用来绑定你在删除或更新操作的时候,页面所传来的ID表示的是哪个字段。
??????????????? this.GridView1.DataBind();
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("<script>alert('数据库连接没打开。');</script>");
??????????? }
??????? }
??????? //我们在GridView的列表中,选择旁边的按钮,然后选择"编辑列",然后选择CommandField选择“删除”,就会出现删除的列,当点击删除的时候,会指定执行RowDeleting
??????? //方法,你双击事件中的该方法,然后开始写代码。
??????? protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
??????? {
??????????? string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();//指定写法,来得到页面传来的删除的ID值
??????????? //要选择GridView1.DataKeys[e.RowIndex].Value的时候,在绑定数据源的时候,一定要申明你绑定的值:this.GridView1.DataKeyNames = new string[] { "ID"};
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
??????????? string sql = "delete from user where id="+Convert.ToInt32(id);
??????????
??????????? con.Open();
??????????? MySqlCommand commend = new MySqlCommand(sql, con);
??????????? commend.ExecuteNonQuery();
??????????? con.Close();
??????????? this.dbBind();
??????? }
??????? //当点击编辑按钮,默认执行事件中的RowEditing方法
??????? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
??????? {
??????????? //当选择编辑按钮,页面传入的ID值的取值方式固定写法。
??????????? this.GridView1.EditIndex = e.NewEditIndex;
??????????? this.dbBind();
??????? }
??????? //当点击取消按钮,默认执行事件中的RowCancelingEdit方法
??????? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
??????? {
??????????? this.GridView1.EditIndex = -1;//当点击取消按钮,只要将EditIndex设置为-1;
??????????? this.dbBind();
??????? }

??????? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
??????? {
??????????? MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");

//取值你要更新列的值。我的列的顺序为:删除,选择,ID,USERNAME,FLAG.

//我要更新的是username,flag,列是从0开始,所以选择的cell为第3列,和第4列。

//要取得你要更新列的指定写法。
??????????? string newName = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
??????????? string newFlag = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim();
??????????? string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
??????????? string sql = "update user set name='"+newName+"',flag='"+newFlag+"' where id="+Convert.ToInt32(id);
??????????? MySqlCommand command = new MySqlCommand(sql,con);
??????????? con.Open();
??????????? command.ExecuteNonQuery();
??????????? con.Close();
??????????? this.GridView1.EditIndex = -1;
??????????? this.dbBind();
??????? }
??? }
}