GridView控件使用

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>GridView控件使用</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" DataKeyNames="EMPNO" OnSorting="GridView1_Sorting" AllowSorting="True" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound">
  11.             <Columns>
  12.                 <asp:BoundField DataField="EMPNO" HeaderText="用户ID" ReadOnly="True" SortExpression="EMPNO" />
  13.                 <asp:BoundField DataField="ENAME" HeaderText="用户名称" SortExpression="ENAME" />
  14.                 <asp:BoundField DataField="JOB" HeaderText="工作" SortExpression="JOB" />
  15.                 <asp:BoundField DataField="SAL" HeaderText="薪水" SortExpression="SAL" />
  16.                 <asp:BoundField DataField="COMM" HeaderText="奖金" SortExpression="COMM" />
  17.                 <asp:CommandField ShowSelectButton="True" HeaderText="选择" />
  18.                 <asp:CommandField ShowEditButton="True" HeaderText="编辑" />
  19.                 <asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
  20.             </Columns>
  21.             <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  22.             <RowStyle BackColor="#E3EAEB" />
  23.             <EditRowStyle BackColor="#7C6F57" />
  24.             <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
  25.             <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
  26.             <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  27.             <AlternatingRowStyle BackColor="White" />
  28.         </asp:GridView>      
  29.     </div>
  30.     </form>
  31. </body>
  32. </html>

 

后台:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.OracleClient;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     protected string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  14.     protected OracleConnection Connection;
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         if (!IsPostBack)
  18.         {
  19.             ViewState["SortOrder"] = "EMPNO";
  20.             ViewState["OrderDire"] = "ASC";
  21.             Bind();
  22.         }
  23.         //删除数据时确认
  24.         //for (int i = 0; i < GridView1.Rows.Count; i++)
  25.         //{
  26.         //    ((LinkButton)GridView1.Rows[i].Cells[7].Controls[0]).Attributes.Add("onClick", "javascript:if(confirm('你真要删除选中的记录吗?')!=1){return false}");
  27.         //}
  28.     }
  29.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消
  30.     {
  31.         GridView1.EditIndex = -1;
  32.         Bind();
  33.     }
  34.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//删除
  35.     {
  36.         string sqlstr = "Delete From emp Where empno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
  37.         Connection = new OracleConnection(ConnectionString);
  38.         OracleCommand cmd = new OracleCommand(sqlstr, Connection);
  39.         Connection.Open();
  40.         cmd.ExecuteNonQuery();
  41.         Connection.Close();
  42.         Bind();
  43.     }
  44.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑
  45.     {
  46.         GridView1.EditIndex = e.NewEditIndex;
  47.         Bind();
  48.     }
  49.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新
  50.     {
  51.         Connection = new OracleConnection(ConnectionString);
  52.         string sqlstr = "Update emp set ename='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
  53.                      + "',job='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()
  54.                      + "',sal='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()
  55.                      + "',comm='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim()
  56.                      + "' Where empno='" + GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim() + "'";
  57.                      //+ "' Where empno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
  58.         OracleCommand cmd = new OracleCommand();
  59.         cmd.Connection = Connection;
  60.         cmd.CommandText = sqlstr;
  61.         Connection.Open();
  62.         cmd.ExecuteNonQuery();
  63.         Connection.Close();
  64.         GridView1.EditIndex = -1;
  65.         Bind();
  66.     }
  67.     protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)//排序
  68.     {
  69.         string sPage = e.SortExpression;
  70.         if (sPage == ViewState["SortOrder"].ToString())
  71.         {
  72.             if (ViewState["OrderDire"].ToString() == "ASC")
  73.             {
  74.                 ViewState["OrderDire"] = "DESC";
  75.             }
  76.             else
  77.             {
  78.                 ViewState["OrderDire"] = "ASC";
  79.             }
  80.         }
  81.         else
  82.         {
  83.             ViewState["SortOrder"] = e.SortExpression;
  84.         }
  85.         Bind();
  86.     }
  87.     public void Bind()//数据绑定
  88.     {
  89.         Connection = new OracleConnection(ConnectionString);
  90.         string sqlstr = "Select empno,ename,job,sal,comm from emp order by " + ViewState["SortOrder"].ToString() + " " + ViewState["OrderDire"].ToString();
  91.         OracleCommand cmd = new OracleCommand(sqlstr, Connection);
  92.         OracleDataAdapter da = new OracleDataAdapter(cmd);
  93.         DataSet ds = new DataSet();
  94.         Connection.Open();
  95.         da.Fill(ds, "用户表");
  96.         Connection.Close();
  97.         GridView1.PageSize = 6;
  98.         GridView1.DataSource = ds.Tables["用户表"];
  99.         GridView1.DataBind();       
  100.     }    
  101.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)//删除数据时确认
  102.     {
  103.         if (e.Row.RowType == DataControlRowType.DataRow)
  104.         {
  105.             if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
  106.             {
  107.                 //((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onClick", "javascript:return confirm('你真要删除用户编号为:/"" + e.Row.Cells[0].Text + "/"的记录吗?')");
  108.                 (e.Row.Cells[7]).Attributes.Add("onClick""javascript:return confirm('你真要删除用户编号为:/"" + e.Row.Cells[0].Text + "/"的记录吗?')");
  109.             }
  110.         }
  111.     }
  112.     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)//分页
  113.     {
  114.         GridView1.PageIndex = e.NewPageIndex;
  115.         Bind();
  116.     }
  117. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值