<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" Theme="Red" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link type="text/css" rel="stylesheet" href="css/style.css" />
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True"
onpageindexchanging="gvEmployee_PageIndexChanging"
onrowdatabound="gvEmployee_RowDataBound" onsorting="gvEmployee_Sorting"
onrowcancelingedit="gvEmployee_RowCancelingEdit"
onrowdeleting="gvEmployee_RowDeleting" onrowediting="gvEmployee_RowEditing"
onrowupdating="gvEmployee_RowUpdating" DataKeyNames="employeeid" PageSize="20">
<Columns>
<asp:BoundField HeaderText="编号" DataField="employeeid" ReadOnly="true" SortExpression="employeeid" ItemStyle-Width="5%"/>
<asp:BoundField HeaderText="姓名" DataField="employeename" SortExpression="employeename" ItemStyle-Width="10%"/>
<asp:BoundField HeaderText="部门" DataField="department" SortExpression="department" ItemStyle-Width="10%"/>
<asp:BoundField HeaderText="地址" DataField="address" ReadOnly="true" SortExpression="address" ItemStyle-Width="15%"/>
<asp:BoundField HeaderText="部门" DataField="department" ReadOnly="true" SortExpression="department" ItemStyle-Width="15%"/>
<asp:BoundField HeaderText="入职时间" DataField="workdate" ReadOnly="true" DataFormatString="{0:yyyy年MM月dd日}" SortExpression="workdate" ItemStyle-Width="15%"/>
<asp:BoundField HeaderText="工资" DataField="salary" ReadOnly="true" DataFormatString="{0:c}" SortExpression="salary" ItemStyle-Width="15%"/>
<asp:CommandField HeaderText="编辑" CancelText="取消" UpdateText="修改" ShowEditButton="true" ItemStyle-Width="5%"/>
<asp:CommandField HeaderText="删除" DeleteText="删除" ShowDeleteButton="true" ItemStyle-Width="5%"/>
<asp:CommandField SelectText="选择" ShowSelectButton="true" ItemStyle-Width="5%"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class Default5 : System.Web.UI.Page
{
private readonly string connString=ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
//绑定数据
public void Bind()
{
SqlConnection conn=new SqlConnection(connString);
string sql="select * from employee";
SqlCommand cmd=new SqlCommand(sql,conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.gvEmployee.DataSource = ds.Tables[0];
this.gvEmployee.DataBind();
}
//初始为数据绑定一些信息
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double salary = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "salary"));
if (salary < 3000)
{
e.Row.ToolTip = "工资低于3000";
}
else
{
e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
e.Row.ToolTip = "工资高于3000";
}
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确定要删除吗');");
((LinkButton)e.Row.Cells[9].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你选择了:\"" + e.Row.Cells[1].Text + "\"')");
}
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#CFCFCF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
}
}
//页码改变的触发事件
protected void gvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvEmployee.PageIndex = e.NewPageIndex;
Bind();
}
//排序
protected void gvEmployee_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression.ToString();
string sortDirection = "ASC";
if (sortExpression == this.gvEmployee.Attributes["SortExpression"])
{
sortDirection = (this.gvEmployee.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
this.gvEmployee.Attributes["SortExpression"] = sortExpression;
this.gvEmployee.Attributes["SortDirection"] = sortDirection;
SortGridView();
}
public void SortGridView()
{
string sortExpression = this.gvEmployee.Attributes["SortExpression"];
string sortDirection = this.gvEmployee.Attributes["SortDirection"];
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter("select * from employee",conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
this.gvEmployee.DataSource = dt;
this.gvEmployee.DataBind();
}
//取消操作
protected void gvEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.gvEmployee.EditIndex = -1;
Bind();
}
//点击编辑按钮触发的事件
protected void gvEmployee_RowEditing(object sender, GridViewEditEventArgs e)
{
this.gvEmployee.EditIndex = e.NewEditIndex;
this.gvEmployee.EditRowStyle.BackColor = System.Drawing.Color.SkyBlue;
Bind();
}
//删除操作
protected void gvEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(connString);
string sql=string.Format("delete from employee where employeeid={0}",this.gvEmployee.DataKeys[e.RowIndex].Value);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
this.gvEmployee.EditIndex = -1;
Bind();
}
//编辑后更新操作
protected void gvEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int employeeid = Convert.ToInt32(this.gvEmployee.DataKeys[e.RowIndex].Value);
string employeename = ((TextBox)(gvEmployee.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string department = ((TextBox)(gvEmployee.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string sql = string.Format("update employee set employeename='{0}',department='{1}' where employeeid={2}", employeename, department, employeeid);
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
this.gvEmployee.EditIndex = -1;
Bind();
}
}