使用GridView控件绑定数据源
GridView控件个人认为就是数据表格控件,它以表格的形式显示数据源中的数据。每列表示一个字段,每行表示一条记录。
GridView控件支持在页面有一下功能:
绑定GridView控件到数据源的有两种方法:通过配置数据源绑定和通过代码绑定。
1.通过配置数据源绑定GridView控件
下面演示通过SqlDataSource控件配置数据源,并连接数据库,然后使用GridView控件绑定SqlDataSource数据源。
数据库表内容如:
新建一个网站,建立一个Inde.aspx页面。在工具箱->数据分类下面拽一个GridView控件和一个SqlDataSource控件出来。如下图:
接下来配置SqlDataScource控件,
接下来就要配置GridView控件了,在GridView控制中选择我们的数据源SqlDataSource控件即可,如下图
配置完成后,运行下我们的效果看下,如下图
2.通过代码绑定GridView控件
同样上面的代码可以用下面的代码完成。加载事件时写法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
using
System; using
System.Collections.Generic; using
System.Data; using
System.Data.SqlClient; using
System.Linq; using
System.Web; using
System.Web.UI; using
System.Web.UI.WebControls; namespace
DataShowApplication { public
partial class Index : System.Web.UI.Page { protected
void Page_Load( object
sender, EventArgs e) { //创建字符串连接 SqlConnection sqlconn = new
SqlConnection ( "Data Source=.\\CENA;Initial Catalog=Asp.net;Persist Security Info=True;User ID=sa;Password=123456" ); SqlDataAdapter sqlda = new
SqlDataAdapter( "select * from DemoStu" , sqlconn); DataSet ds = new
DataSet (); sqlda.Fill(ds); this .Data_GridView.DataSource = ds; this .Data_GridView.DataBind(); } } } |
自定义GridView控件的列
GridView控件中的每一列有一个DataControlField对象。
实验如何在在GridView控件中添加BoundField列,从而进行数据绑定实例如下:
添加一个Index.aspx页面,添加一个GridView控件和一个SqlDataSoure控件。如下图:
要对GridView控件经行自定义列,要先取消GridView自动产生字段的功能。把GridView的AutoGeneraterColums属性设置为false就可以了。
然后设置SqlDataSource控件配置数据源,并将SqlDataSource控件指定给GridView控件的DataSourceID属性。如下图:
然后编辑GridView控件,如下图:
看下运行结果,如下图:
使用GridView控件分页显示数据
GridView控件有一个内置的分页控件,可以支持基本的分页功能。启用分页需要设置AllowPaging,PageSize属性。如下图:
然后需要写一个GridView控件的PageIndexChanging事件,写法如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DataShowApplication { public partial class IndexO : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridViewOne_PageIndexChanging(object sender, GridViewPageEventArgs e) { //获取当前分页的索引值 this.GridViewOne.PageIndex = e.NewPageIndex; this.GridViewOne.DataBind(); } } }
写完运行如下结果:
GridView在编程中的一点高级应用
GridView中其实还包含了一族好玩的东西,就是RowEdtiting,RowUpdating,RowCancelingEdit事件,这些事件可以帮助我们实现编辑,更新和取消操作,还有一个RowDeleting事件,可以实现删除某条记录的操作。下面可以演示过程:
1.新建一个IndexTow.aspx页面放一个GridView控件进去,然后利用GridView控件的CommandField列的选择,编辑,取消和删除按钮。
现在开始我们的操作,先绑定数据。
当然加载的时候我们就应该绑定上数据,绑定数据代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 |
using
System; using
System.Collections.Generic; using
System.Data; using
System.Data.SqlClient; using
System.Linq; using
System.Web; using
System.Web.UI; using
System.Web.UI.WebControls; namespace
DataShowApplication { public
partial class IndexTwo : System.Web.UI.Page { protected
void Page_Load( object
sender, EventArgs e) { if
(!IsPostBack) { //调用绑定方法 this .BindData(); } } //写一个绑定数据的方法 private
void BindData() { //老一套连接字符串先走起 string
strconn = @"Data Source=.\CENA;Initial Catalog=Asp.net;Persist Security Info=True;User ID=sa;Password=123456" ; //写SQL 有SQL语句了才能有数据(其实标准的SQL语句基本都是大写的看大牛一般都那样写) string
sqlquery = "select * from DemoStu" ; //准备好了就要创建数据库连接对象 SqlConnection connection = new
SqlConnection(strconn); //然后在搞一个数据适配器 SqlDataAdapter da = new
SqlDataAdapter(sqlquery, strconn); //继续创建数据集合 DataSet ds = new
DataSet(); //填充数据集 da.Fill(ds); //设置咱GridView控件数据源为创建的数据集ds this .GridViewTwo.DataSource = ds; //将数据库表中的主键字段放入GridView控件的DataKeyNames属性中 this .GridViewTwo.DataKeyNames = new
string [] { "ID"
}; //绑定数据库中的数据 this .GridViewTwo.DataBind(); } } } |
绑定好数据后,我们运行结果如下:
然后我们继续完善,先来搞编辑,当我们点击编辑时,应该触发GridView控件的RowEditing事件,这时我们的编辑项索引设置应当为当前选择的项的索引,然后从新绑定数据。代码如下:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace DataShowApplication { public partial class IndexTwo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //调用绑定方法 this.BindData(); } } //写一个绑定数据的方法 private void BindData() { //老一套连接字符串先走起 string strconn = @"Data Source=.\CENA;Initial Catalog=Asp.net;Persist Security Info=True;User ID=sa;Password=123456"; //写SQL 有SQL语句了才能有数据(其实标准的SQL语句基本都是大写的看大牛一般都那样写) string sqlquery = "select * from DemoStu"; //准备好了就要创建数据库连接对象 SqlConnection connection = new SqlConnection(strconn); //然后在搞一个数据适配器 SqlDataAdapter da = new SqlDataAdapter(sqlquery, strconn); //继续创建数据集合 DataSet ds = new DataSet(); //填充数据集 da.Fill(ds); //设置咱GridView控件数据源为创建的数据集ds this.GridViewTwo.DataSource = ds; //将数据库表中的主键字段放入GridView控件的DataKeyNames属性中 this.GridViewTwo.DataKeyNames = new string[] { "ID" }; //绑定数据库中的数据 this.GridViewTwo.DataBind(); } protected void RowEditing(object sender, GridViewEditEventArgs e) { //设置索引值 this.GridViewTwo.EditIndex = e.NewEditIndex; this.BindData(); } protected void GridViewTwo_RowUpdating(object sender, GridViewUpdatedEventArgs e) { //取得编辑行的关键字段的值 string ID = this.GridViewTwo.DataKeys[e.RowIndex].Value.ToString(); //取得文本框中输入的内容 string Name = ((TextBox)(this.GridViewTwo.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); string Price = ((TextBox)(this.GridViewTwo.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); string time = ((TextBox)(this.GridViewTwo.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); //定义更新操作的SQL语句 string update_sql = "update DemoStu set Name=‘" + Name + "‘,Price=‘" + Price + "‘,Time=‘" + time + "‘ where id=‘" + ID + "‘"; bool update = ExceSQL(update_sql);//调用ExceSQL执行更新操作 if (update) { Response.Write("<script language=javascript>alert(‘修改成功!‘)</script>"); //设置GridView控件的编辑项的索引为-1,即取消编辑 this.GridViewTwo.EditIndex = -1; BindData(); } else { Response.Write("<script language=javascript>alert(‘修改失败!‘);</script>"); } } protected void GridViewTwo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { //设置GridView控件的编辑项的索引为-1,即取消编辑 this.GridViewTwo.EditIndex = -1; BindData(); } protected void GridViewTwo_RowDeleting(object sender, GridViewDeletedEventArgs e) { string delete_sql = "delete from DemoStu where id=‘" + this.GridViewTwo.DataKeys[e.RowIndex].Value.ToString() + "‘"; bool delete = ExceSQL(delete_sql);//调用ExceSQL执行删除操作 if (delete) { Response.Write("<script language=javascript>alert(‘删除成功!‘)</script>"); BindData();//调用自定义方法重新绑定控件中数据 } else { Response.Write("<script language=javascript>alert(‘删除失败!‘)</script>"); } } /// <summary> /// 执行SQL语句的方法 /// </summary> /// <param name="update_sql"></param> /// <returns></returns> private bool ExceSQL(string update_sql) { //定义数据库连接字符串 string strCon = @"Data Source=.\CENA;Initial Catalog=Asp.net;Persist Security Info=True;User ID=sa;Password=123456"; //创建数据库连接对象 SqlConnection sqlcon = new SqlConnection(strCon); SqlCommand sqlcom = new SqlCommand(update_sql, sqlcon); try { if (sqlcon.State == System.Data.ConnectionState.Closed)//判断数据库是否为连连状态 { sqlcon.Open(); } sqlcom.ExecuteNonQuery();//执行SQL语句 return true; } catch { return false; } finally { sqlcon.Close();//关闭数据库连接 } } } }
关于GridView就简单的介绍到这里,跟我一样的小虾可以自己动手去实验一下。。。。。
后面在继续初始学习我们的另外2个常用数据控件分别是DataList控件,ListView控件。
初始ADO.NET数据控件GridView,布布扣,bubuko.com
原文:http://www.cnblogs.com/HuiTai/p/huitai_Net9.html