using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;//ArrayList命名空间
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//正常换行
//GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
//自动换行
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
if (!IsPostBack)
{
//某个字段---stuID--
ViewState["SortOrder"] = "stuID";
ViewState["OrderDire"] = "ASC";
GridViewBind();
//往DropDownList1填入数据
GetDropValue();
}
}
//DropDownList填值
public void GetDropValue()
{
ArrayList array = new ArrayList();
array.Add("");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
array.Add(GridView1.Rows[i].Cells[2].Text);
}
DropDownList1.DataSource = array;
DropDownList1.DataBind();
}
string strConnect = ConfigurationManager.AppSettings["ConnectString"].ToString();
public void GridViewBind()
{
//实例化SqlConnection对象
SqlConnection sqlCon = new SqlConnection();
//实例化SqlConnection对象连接数据库的字符串
sqlCon.ConnectionString = strConnect;
string SqlStr = "select * from tb_StuInfo";
//实例化SqlDataAdapter对象
SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
//实例化数据集DataSet
DataSet ds = new DataSet();
da.Fill(ds, "tb_StuInfo");
DataView dv = ds.Tables[0].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
dv.Sort = sort;
//绑定DataList控件
GridView1.DataKeyNames = new string[] { "stuID" };
//暂不启用
GridView1.PageSize = 3;
GridView1.DataSource = dv;//设置数据源,用于填充控件中的项的值列表
GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
//设置主键
}
//排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
GridViewBind();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection myConnection = new SqlConnection(strConnect);
myConnection.Open();
string stuName=((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
string stuSex=((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
string stuHobby=((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
string stuID = GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlCommand myCommand = new SqlCommand("update tb_StuInfo set stuName='"+stuName+"',stuSex='"+stuSex+"',stuHobby='"+stuHobby+"' where stuID='"+stuID+"'",myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
//不会将编辑行的索引保持在更新行
GridView1.EditIndex = -1;
//记得绑定
GridViewBind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
//加入JS
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//数据行的话
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标放上去的样式
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.backgroundColor='#533689'");
//鼠标移开
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection myConnection = new SqlConnection(strConnect);
myConnection.Open();
SqlCommand myCommand = new SqlCommand("delete tb_StuInfo where stuID='"+GridView1.DataKeys[e.RowIndex].Value+"'",myConnection);
myCommand.ExecuteNonQuery();
//索引
GridView1.EditIndex = -1;
GridViewBind();
}
//全选
//记得将CheckBox2---AutoPostBack设置为True
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
// Response.Write("test");
}
//将已经选择复选框的行删除
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(strConnect);
SqlCommand myCommand;
for (int i = 0;i < GridView1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
myCommand = new SqlCommand("delete tb_StuInfo where stuID='" + GridView1.DataKeys[i].Value + "'", myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
GridViewBind();
}
//DropDownList选择事件
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
row.Cells[2].BackColor = System.Drawing.Color.White;
}
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
if (DropDownList1.Items[i].Selected == true)
{
string stuID = DropDownList1.Items[i].Text;
for (int j = 0; j < GridView1.Rows.Count; j++)
{
if (GridView1.Rows[j].Cells[2].Text == stuID)
{
GridView1.Rows[j].Cells[2].BackColor = System.Drawing.Color.Red;
}
}
break;
}
}
}
//取消全选
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
cbox.Checked = false;
CheckBox2.Checked = false;
}
}
分页功能
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
}
//Default.aspx
----------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div runat="server">
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
onsorting="GridView1_Sorting"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting" AllowPaging="true" onpageindexchanging="GridView1_PageIndexChanging"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="stuID" HeaderText="学号" SortExpression="stuID" />
<asp:BoundField DataField="stuName" HeaderText="姓名" SortExpression="stuName" />
<asp:BoundField DataField="stuSex" HeaderText="性别" SortExpression="stuSex" />
<asp:BoundField DataField="stuHobby" HeaderText="爱好"
SortExpression="stuHobby" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField DeleteText="<div οnclick="return confirm('确定删除?')">删除<div>"
HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:CheckBox ID="CheckBox2" runat="server"
oncheckedchanged="CheckBox2_CheckedChanged" Text="全选"
AutoPostBack="True" />
<asp:Button ID="Button1" runat="server" Text="取消" οnclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="删除" οnclick="Button2_Click"
style="height: 26px" />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
>
</asp:DropDownList>
</div>
</form>
</body>
</html>
----------------------------------------------------------------
--相关字段资料
转载于:https://www.cnblogs.com/sanpi/archive/2010/01/23/1654842.html