350 likes | 485 Views
GridView 控件. æ•°æ®åº“: Information æ•°æ®è¡¨ï¼š mytable. GridView 控件. æ•°æ®è®¿é—®ç±» using System.Data.SqlClient; public class DataAccess { public DataAccess() { // // TODO: 在æ¤å¤„æ·»åŠ æž„é€ å‡½æ•°é€»è¾‘ // } SqlConnection con = new SqlConnection();
E N D
GridView控件 数据库:Information 数据表:mytable
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>
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; • }
GridView控件 1、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>
//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(); • }
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(); • }
//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>
//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(); • }
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(); • }
//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" />
//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; • } • } • }
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; • } • }
//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>
//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(); • } • }
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'"); • } } • }
5、GridView实现自动编号: 原有的用户ID呢?
//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>
//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(); • } • } • 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(); • } • }
//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>
//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); • } • } • }
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(); • } • }
//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" />
//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; • }
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; • }
//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>
//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>
//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(); • } • }