Asp.Net 控件 GridView

转自http://blog.csdn.net/u010940770/article/details/16106181


这两天做的作业都得用到visual studio 越来越发现其功能真心强大

前几天Asp.Net做了个界面增删查改的作业(连接数据库),用到了个组件GridView,感觉很强大

在这里小结一下(这里主要说下字段和事件):

字段,
BoundField 显示数据源中某个字段的值。这是 GridView控件的默认列类型。
ButtonField 为 GridView控件中的每个项显示一个命令按钮。可以创建一列自定义按钮控件,如“添加”按钮或“移除”按钮。
CheckBoxField 为 GridView控件中的每一项显示一个复选框。此列字段类型通常用于显示具有布尔值的字段。
CommandField  显示用来执行选择、编辑或删除操作的预定义命令按钮。
HyperLinkField 将数据源中某个字段的值显示为超链接。此列字段类型允许您将另一个字段绑定到超链接的 URL。
ImageField 为 GridView控件中的每一项显示一个图像。
TemplateField 根据指定的模板为 GridView控件中的每一项显示用户定义的内容。此列字段类型允许您创建自定义的列字段。


事件,
RowCancelingEdit 在一个处于编辑模式的行的Cancel按钮被单击,但是在该行退出编辑模式之前发生。
RowCommand 单击一个按钮时发生。
RowCreated 创建一行时发生。 
RowDataBound 一个数据行绑定到数据时发生。
RowDeleting, RowDeleted 这两个事件都是在一行的Delete按钮被单击时发生。它们分别在该网格控件删除
该行之前和之后激发。
RowEditing 当一行的Edit按钮被单击时,但是在该控件进入编辑模式之前发生。
RowUpdating,RowUpdated 这两个事件都是在一行的Update按钮被单击时发生。它们分别在该网格控件更
新该行之前和之后激发。
SelectedIndexChanging, SelectedIndexChanged这两个事件都是在一行的Select按钮被单击时发生。
它们分别在该网格控件处理选择操作之前和之后激发。 
Sorting, Sorted 这两个事件都是在对一个列进行排序的超链接被单击时发生。
它们分别在网格控件处理排序操作之前和之后激发

接下来把作业拿出来分析几个事件:

之前我已说过如何连接Mysql数据库,这个界面增删查改作业也是基于那个基础,当然也是之前那个界面

我把添加页面写在同一个界面上,理解就行,代码:

界面,

[csharp]  view plain copy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DataRefresh.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  8.     <title>界面增删查改</title>  
  9. </head>  
  10. <body style="height: 34px">  
  11.       
  12.     <form id="form1" runat="server">  
  13.   
  14.         <div id="container">  
  15.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Font-Size="12px" Width="591px" CellPadding="4" ForeColor="#333333" GridLines="None"  
  16.             OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand"  
  17.             OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"  
  18.             >  
  19.             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  20.           <Columns>  
  21.               
  22.             <asp:BoundField DataField="s_no" HeaderText="学号" />  
  23.             <asp:BoundField DataField="s_name" HeaderText="名字" />  
  24.             <asp:BoundField DataField="s_age" HeaderText="年龄" />  
  25.             <asp:BoundField DataField="s_sex" HeaderText="性别" />  
  26.             <asp:CommandField HeaderText="编辑" ShowEditButton="true"/>  
  27.               <asp:CommandField HeaderText="删除" ShowDeleteButton="true">  
  28.                     
  29.               </asp:CommandField>  
  30.                 
  31.           </Columns>  
  32.               
  33.             <EditRowStyle BackColor="#999999" />  
  34.             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  35.             <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  36.             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  37.             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  38.             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  39.             <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  40.             <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  41.             <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  42.             <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  43.               
  44.         </asp:GridView>  
  45.     
  46.          <br />  
  47.         <br />  
  48.          
  49.          学号:  
  50.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />  
  51.         名字:  
  52.         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />  
  53.         年龄:  
  54.         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />  
  55.         性别:  
  56.         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
  57.          <p>  
  58.             <asp:Button ID="Btn_add" runat="server" Text="添加" OnClick="Btn_add_Click" />  
  59.         </p>  
  60.   
  61.         </div>  
  62.     </form>  
  63. </body>  
  64. </html>  

实现类,

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using MySql.Data.MySqlClient;  
  11.   
  12.   
  13. namespace DataRefresh  
  14. {  
  15.     public partial class WebForm1 : System.Web.UI.Page  
  16.     {  
  17.         MySqlConnection mySqlConn;//mysql连接对象  
  18.   
  19.         protected void Page_Load(object sender, EventArgs e)  
  20.         {  
  21.             string connStr = "Database=myschool;Data Source=localhost;User Id=root;Password=123";//连接字符串  
  22.             mySqlConn = new MySqlConnection(connStr);  
  23.             mySqlConn.Open();  
  24.             if (!IsPostBack)  
  25.             {  
  26.                 bind();  
  27.             }  
  28.         }  
  29.   
  30.         public void bind()  
  31.         {  
  32.             //创建MySqlDataAdapter对象执行查询  
  33.             MySqlDataAdapter DataAdapter = new MySqlDataAdapter("select * from student", mySqlConn);  
  34.             DataSet dataset = new DataSet();  
  35.             // 填充DataSet对象  
  36.             DataAdapter.Fill(dataset, "student");  
  37.             //将数据显示在gridview中  
  38.             GridView1.DataSource = dataset;  
  39.             GridView1.DataKeyNames = new string[] { "s_no" };//主键  
  40.             GridView1.DataBind();  
  41.               
  42.         }  
  43.   
  44.   
  45.         //删除  
  46.         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  47.         {  
  48.             string sqlStr = "delete from student where s_no=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + ";";  
  49.             //System.Diagnostics.Debug.Write(sqlStr);  
  50.             MySqlCommand mySqlCmd = new MySqlCommand(sqlStr,mySqlConn);  
  51.             mySqlCmd.ExecuteNonQuery();  
  52.             bind();  
  53.         }  
  54.         //gridview改变的时候设置每一行的id  
  55.         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)  
  56.         {  
  57.             int id = Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString());  
  58.         }  
  59.         //获取事件  
  60.         protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  61.         {  
  62.             //System.Diagnostics.Debug.Write(e.CommandName);  
  63.         }  
  64.         //更新  
  65.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  66.         {  
  67.             string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();  
  68.             GridViewRow gvr = GridView1.Rows[e.RowIndex];  
  69.             string s_no = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();  
  70.             string s_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();  
  71.             string s_age = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();  
  72.             string s_sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();  
  73.   
  74.             string sql = "update student set s_no='" + s_no + "',s_name='" + s_name + "',s_age='" + s_age + "',s_sex='" + s_sex + "' where s_no=" + ID + ";";  
  75.             System.Diagnostics.Debug.Write(sql);  
  76.             MySqlCommand mySqlCmd = new MySqlCommand(sql, mySqlConn);  
  77.             mySqlCmd.ExecuteNonQuery();  
  78.             bind();  
  79.         }  
  80.         //取消  
  81.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  82.         {  
  83.             GridView1.EditIndex = -1;  
  84.             bind();  
  85.         }  
  86.         //编辑  
  87.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  88.         {  
  89.             //int index = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);  
  90.             GridView1.EditIndex = e.NewEditIndex;  
  91.             bind();  
  92.   
  93.         }  
  94.          
  95.   
  96.         //添加  
  97.         protected void Btn_add_Click(object sender, EventArgs e)  
  98.         {  
  99.             //Response.Redirect("WebForm2.aspx"); 跳转  
  100.   
  101.             int sno = int.Parse(TextBox1.Text);  
  102.             int age = int.Parse(TextBox3.Text);  
  103.   
  104.             string sql = "insert into student(s_no,s_name,s_age,s_sex) values('"+sno+"' ,'" + TextBox2.Text + "','" + age + "','" + TextBox4.Text + "');";  
  105.             //System.Diagnostics.Debug.Write(sql);  
  106.             MySqlCommand mySqlCmd = new MySqlCommand(sql, mySqlConn);  
  107.             mySqlCmd.ExecuteNonQuery();  
  108.             bind();  
  109.   
  110.             TextBox1.Text = "";  
  111.             TextBox2.Text = "";  
  112.             TextBox3.Text = "";  
  113.             TextBox4.Text = "";  
  114.         }  
  115.     }  
  116. }  

效果,

这个简单地界面中实现了增(Btn_add_Click),删(GridView1_RowDeleting),编辑(GridView1_RowUpdating GridView1_RowEditing GridView1_RowCancelingEdit);其中用到了5个事件,实现了删除和编辑。这些事件只需

要在GridView(界面)中注册然后在代码里实现就可以了,GridView的强大之处还在于它能适用于各种数据库!

觉得几点重要的地方:

1. bind()方法,实现了数据与GridView的绑定,之后主要起刷新作用

2. 在Page_Load()中调用bind()方法时必须加判断if (!IsPostBack),作用是看GridView中的数据是否发生改变

3. 在web项目中输出语句得用   System.Diagnostics.Debug.WriteLine();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值