gridview 控件

35
GridView 控控 控控控Information 控控控mytable 控控控 控控控控 控控 UserID nvarchar(5 0) 控控控控 UserName nvarchar(5 0) 控控控 UserSex bit 控控 Address nvarchar(5 控控控控

Upload: naava

Post on 20-Jan-2016

153 views

Category:

Documents


0 download

DESCRIPTION

GridView 控件. 数据库: Information 数据表: mytable. GridView 控件. 数据访问类 using System.Data.SqlClient; public class DataAccess { public DataAccess() { // // TODO: 在此处添加构造函数逻辑 // } SqlConnection con = new SqlConnection(); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: GridView 控件

GridView 控件

数据库: Information

数据表: mytable

字段名 数据类型 描述UserID nvarchar(50) 用户编号UserName nvarchar(50) 用户名UserSex bit 性别Address nvarchar(50) 家庭地址

Page 2: GridView 控件

GridView 控件数据访问类using System.Data.SqlClient; public class DataAccess { public DataAccess() { // // TODO: 在此处添加构造函数逻辑 // } SqlConnection con = new SqlConnection(); string constr = "server=(local);database=Information;uid=sa;pwd=ccutsoft"; public void CheckOpen() { con.ConnectionString = constr; if(con.State==ConnectionState.Closed ) con.Open(); } /// <summary>

Page 3: GridView 控件

GridView 控件 /// 执行 SQL语句,返回整型数据, 0为执行失败,非 0为执行成功 /// </summary> /// <param name="sqlstr">sql语句 </param> /// <returns>i</returns> public int ExecuteSQL(string sqlstr) { CheckOpen(); SqlCommand com = new SqlCommand(sqlstr,con); int i = com.ExecuteNonQuery(); CloseOpen(); return i; } /// <summary> /// 执行 SQL语句,返回数据集 /// </summary> /// <param name="sqlstr">SQL语句 </param> /// <param name="table_name">数据集中数据表名 </param> /// <returns>dst</returns> public DataSet getDataSet(string sqlstr,string table_name) { CheckOpen(); SqlDataAdapter ada = new SqlDataAdapter(sqlstr, con); DataSet dst = new DataSet(); ada.Fill(dst, table_name); CloseOpen(); return dst; }

Page 4: GridView 控件

GridView 控件

1 、 GridView 选中,删除:

Page 5: GridView 控件

//aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" Font-Size="9pt">

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID" ReadOnly="True" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 " /> <asp:CommandField HeaderText="删除 " ShowDeleteButton="True" /> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>

Page 6: GridView 控件

//aspx.cs

DataAccess da = new DataAccess(); protected void Page_Load(object sender, EventArgs e) { BindGriView(); } void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str,"tables"); GridView1.DataSource = dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[] { "userid"}; GridView1.DataBind(); }

Page 7: GridView 控件

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{ string sqlstr = "delete from mytable where

userid='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

int i = da.ExecuteSQL(sqlstr ); BindGriView();

}

Page 8: GridView 控件

2 、 GridView 正反双向排序:

Page 9: GridView 控件

//aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

CellPadding="3" Font-Size="9pt" OnSorting="GridView1_Sorting" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID" ReadOnly="True"

SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 " /> <asp:CommandField HeaderText="删除 " ShowDeleteButton="True" /> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>

Page 10: GridView 控件

//aspx.cs DataAccess da = new DataAccess(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["SortOrder"] = "UserID"; ViewState["OrderDire"] = "ASC"; bind(); } } void bind() { string str = "select * from mytable"; DataSet dst = da.getDataSet(str,"mytable"); DataView view = dst.Tables["mytable"].DefaultView; string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"]; view.Sort = sort; GridView1.DataSource = view; GridView1.DataBind(); }

Page 11: GridView 控件

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; } bind(); }

Page 12: GridView 控件

3 、全部选中删除

Page 13: GridView 控件

//aspx <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

DataKeyNames="userid" CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC"

BorderStyle="None" BorderWidth="1px"> <FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:TemplateField > <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="UserID" HeaderText="用户 ID" ReadOnly="True"

SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 " SortExpression="Address"/>

</Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" Text="全选 "

OnCheckedChanged="CheckBox2_CheckedChanged" /> <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消 " OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除 " OnClick="Button2_Click" />

Page 14: GridView 控件

//aspx.cs DataAccess da = new DataAccess(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGriView(); } } void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str, "tables"); GridView1.DataSource = dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[] { "userid" }; GridView1.DataBind(); } protected void CheckBox2_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (CheckBox2.Checked == true) { cbox.Checked = true; } else { cbox.Checked = false; } } }

Page 15: GridView 控件

protected void Button2_Click(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked) { string sqlstr = "delete from mytable where userid='" +

GridView1.Rows[i].Cells[1].Text + "'"; int result = da.ExecuteSQL(sqlstr); } } BindGriView(); } protected void Button1_Click(object sender, EventArgs e) { CheckBox2.Checked = false; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); cbox.Checked = false; }

}

Page 16: GridView 控件

4 、鼠标移到 GridView 某一行时改变该行的背景色方法

Page 17: GridView 控件

//aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="userid"

CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound">

<FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID" ReadOnly="True"

SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 "

SortExpression="Address"/> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>

Page 18: GridView 控件

//aspx.cs

DataAccess da = new DataAccess(); void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str, "tables"); GridView1.DataSource = dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[] { "userid" }; GridView1.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGriView(); } }

Page 19: GridView 控件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{ if (e.Row.RowType ==

DataControlRowType.DataRow) { // 鼠标经过时,行背景色变 e.Row.Attributes.Add("onmouseover",

"this.style.backgroundColor='#E6F5FA'"); // 鼠标移出时,行背景色变 e.Row.Attributes.Add("onmouseout",

"this.style.backgroundColor='#FFFFFF'"); } } }

Page 20: GridView 控件

5 、 GridView 实现自动编号:

原有的用户 ID呢 ?

Page 21: GridView 控件

//aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="userid"

CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound">

<FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID" ReadOnly="True"

SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 "

SortExpression="Address"/> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>

Page 22: GridView 控件

//aspx.cs

DataAccess da = new DataAccess(); void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str, "tables"); GridView1.DataSource =

dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[]

{ "userid" }; GridView1.DataBind(); }

Page 23: GridView 控件

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGriView(); } } protected void GridView1_RowDataBound(object sender,

GridViewRowEventArgs e) { if (e.Row.RowIndex != -1) { int id = e.Row.RowIndex + 1; e.Row.Cells[0].Text = id.ToString(); } }

Page 24: GridView 控件

6 、 GridView 实现用“ ...” 代替超长字符串:

Page 25: GridView 控件

//aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="userid"

CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">

<FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID"

ReadOnly="True" SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 "

SortExpression="Address"/> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True"

ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"

/> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>

Page 26: GridView 控件

//aspx.cs DataAccess da = new DataAccess(); void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str, "tables"); GridView1.DataSource = dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[] { "userid" }; GridView1.DataBind(); for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { DataRowView mydrv; string gIntro; if (GridView1.PageIndex == 0) { mydrv = dst.Tables["tables"].DefaultView[i]; gIntro = Convert.ToString(mydrv["Address"]); GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } } }

Page 27: GridView 控件

public string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; } string sNewStr = sString.Substring(0, nLeng); sNewStr = sNewStr + "..."; return sNewStr; }

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGriView(); } }

Page 28: GridView 控件

7 、 GridView显示隐藏某一列

Page 29: GridView 控件

//aspx <asp:GridView ID="GridView1" runat="server" AllowSorting="True"

AutoGenerateColumns="False" DataKeyNames="userid" CellPadding="3" Font-Size="9pt" BackColor="White"

BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"> <FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID" ReadOnly="True"

SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 "

SortExpression="Address"/> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True"

ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"

/> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Font-

Size="12px" Text="显示隐藏家庭住址 " OnCheckedChanged="CheckBox1_CheckedChanged" />

Page 30: GridView 控件

//aspx.cs

DataAccess da = new DataAccess(); void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str, "tables"); GridView1.DataSource = dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[] { "userid" }; GridView1.DataBind(); GridView1.Columns[3].Visible = false; CheckBox1.Checked = false; }

Page 31: GridView 控件

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGriView(); } } protected void CheckBox1_CheckedChanged(object sender,

EventArgs e) { GridView1.Columns[3].Visible = !GridView1.Columns[3].Visible; }

Page 32: GridView 控件

8 、 GridView 固定表头

Page 33: GridView 控件

//aspx 1 、设置样式 <title> ……. <style type="text/css" > .Freezing { position:relative ; table-layout:fixed; top:expression(this.offsetParent.scrollTop); z-index: 10; }

.Freezing th{...}{text-overflow:ellipsis;overflow:hidden;white-space: nowrap;padding:2px;}

</style> </title>

Page 34: GridView 控件

//aspx <div style="height: 100px;width:350px; overflow:scroll " id="dvBody"> <asp:GridView ID="GridView1" runat="server" AllowSorting="True"

AutoGenerateColumns="False" DataKeyNames="userid" CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC"

BorderStyle="None" BorderWidth="1px"> <FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户 ID"

ReadOnly="True" SortExpression="UserID" /> <asp:BoundField DataField="UserName" HeaderText="用户姓名 " /> <asp:BoundField DataField="UserSex" HeaderText="性别 " /> <asp:BoundField DataField="Address" HeaderText="家庭住址 "

SortExpression="Address"/> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True"

ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"

CssClass="Freezing" /> </asp:GridView> </div>

Page 35: GridView 控件

//aspx.cs

DataAccess da = new DataAccess(); void BindGriView() { string str = "select * from mytable"; DataSet dst = new DataSet(); dst = da.getDataSet(str, "tables"); GridView1.DataSource = dst.Tables["tables"].DefaultView; GridView1.DataKeyNames = new string[] { "userid" }; GridView1.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGriView(); } }