服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - ASP.NET数据绑定GridView控件使用技巧

ASP.NET数据绑定GridView控件使用技巧

2019-12-30 13:35lijiao ASP.NET教程

这篇文章主要为大家详细介绍了ASP.NET数据绑定GridView控件使用技巧,感兴趣的小伙伴们可以参考一下

不得不说GridView控件的功能确实很强大,一个简简单单的控件就可以把数据管理的很美。在这两天做的任务中碰到的一些GridView控件中遇到的问题进行总结;

①:在GridView控件中随意显示数据库中的信息:

GridView控件中有一个AutoGenerateColumns属性,它的作用就是控制GridView控件是否在运行的时候自动生成相关联的列,一般情况下把这个属性设置成为false。因为我们需要的是一个DIY的GridView控件。然后点击右上角的箭头,选择编辑列添加一个BoundField字段,选择数据DataField属性,在后面填上自己想要显示数据库中某一列的列名,在外观HeaderText属性中填写数据库中要显示的列名加以提示。然后点击确定控件中就会显示如下图所示:

 ASP.NET数据绑定GridView控件使用技巧

然后在asp后台中添加链接数据库代码就ok了。关于链接数据库的代码博主在博文“【ASP】用GRIDVIEW控件连接SQL SERVER数据库”中已经做了详细介绍,本文就不多说了。

②:在GridView控件中实现编辑删除的功能:

点击GridView控件右上角的箭头,选择编辑列,添加CommandField字段,设置此字段行为属性ShowDeleteButton和ShowEditButton为True。点击确定即可。结果如图下所示:

 ASP.NET数据绑定GridView控件使用技巧

 但是此时的编辑删除不会有任何功能。因为GridView控件中有好多事件,实现编辑删除功能是要触发相应的事件才可以用。

首先介绍第一个事件——RowEditing。运行页面的时候点击编辑会出现“更改”和“取消”。此事件的作用就是点击编辑时可以显示更新和取消。 RowCancelingEdit。运行页面的时候点击编辑会出现“更改”和“取消”,运行结果如下图所示:

 ASP.NET数据绑定GridView控件使用技巧

双击此事件,在后台添加代码如下:

  1. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
  2.  
  3.  { 
  4.  
  5.    GridView1.EditIndex = e.NewEditIndex; 
  6.  
  7.    this.shuaxin(); 
  8.  
  9.  } 

第二个事件——RowCancelingEdit       事件RowCancelingEdit就是实现取消功能。双击此事件填写代码如下:

  1. protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
  2.  
  3.  
  4.  GridView1.EditIndex = -1; 
  5.  
  6.  this.shuaxin(); 
  7.  

第三个事件——RowUpdating实现更新功能,双击此事件添加代码如下:

  1. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
  2.  
  3.  
  4.   this.GridView1.EditIndex = e.RowIndex; 
  5.  
  6.   string title = GridView1.DataKeys[e.RowIndex].Value.ToString(); 
  7.  
  8.   string cotent = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; 
  9.  
  10.  
  11.  
  12.   string strsql = "update activities set cotent='" + cotent + "' 
  13.  
  14.            where title='" + title + "'"; 
  15.  
  16.   SqlConnection con = new SqlConnection(ConfigurationManager. 
  17.  
  18.             ConnectionStrings["username"].ConnectionString); 
  19.  
  20.   SqlCommand cmd = new SqlCommand(strsql, con); 
  21.  
  22.   con.Open(); 
  23.  
  24.   cmd.ExecuteNonQuery(); 
  25.  
  26.   con.Close(); 
  27.  
  28.   GridView1.EditIndex = -1; 
  29.  
  30.   this.shuaxin(); 
  31.  

第四个事件——RowDeleting。此事件实现删除功能,双击事件添加代码如下:

  1. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
  2.  
  3.  
  4.   string title = GridView1.DataKeys[e.RowIndex].Value.ToString(); 
  5.  
  6.  
  7.  
  8.   string delete = "delete activities where title='" + title + "'"
  9.  
  10.  
  11.  
  12.   SqlConnection con = new SqlConnection(ConfigurationManager. 
  13.  
  14.             ConnectionStrings["username"].ConnectionString); 
  15.  
  16.   SqlCommand cmd = new SqlCommand(delete, con); 
  17.  
  18.   con.Open(); 
  19.  
  20.   cmd.ExecuteNonQuery(); 
  21.  
  22.   con.Close(); 
  23.  
  24.   GridView1.EditIndex = -1; 
  25.  
  26.   this.shuaxin();  //自己写的链接数据库的方法; 
  27.  

附:shuaxin();代码:

  1. private void shuaxin() 
  2.  
  3.  
  4.   SqlConnection sqlcon = new SqlConnection(ConfigurationManager. 
  5.  
  6.              ConnectionStrings["username"].ConnectionString); 
  7.  
  8.   sqlcon.Open(); 
  9.  
  10.   SqlDataAdapter da = new SqlDataAdapter(@"select * from activities", sqlcon); 
  11.  
  12.   DataSet ds = new DataSet(); 
  13.  
  14.   da.Fill(ds); 
  15.  
  16.   if (ds.Tables[0].Rows.Count > 0) 
  17.  
  18.   { 
  19.  
  20.     GridView1.DataSource = ds; 
  21.  
  22.     GridView1.DataBind(); 
  23.  
  24.   } 
  25.  
  26.   sqlcon.Close(); 
  27.  

注:GridView控件中有一个DataKeyNames属性,设置datakeyname是要在点击行时获得该行数据的主键,

以保证删除更新时准确性;若没有设置此属性就会出现如下结果:

ASP.NET数据绑定GridView控件使用技巧

以上就是关于ASP.NET数据绑定GridView控件使用技巧,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐