1 / 35

GridView 控件

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

naava
Download Presentation

GridView 控件

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GridView控件 数据库:Information 数据表:mytable

  2. 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>

  3. 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; • }

  4. GridView控件 1、GridView选中,删除:

  5. //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>

  6. //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(); • }

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

  8. 2、GridView正反双向排序:

  9. //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>

  10. //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(); • }

  11. 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(); • }

  12. 3、全部选中删除

  13. //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" />

  14. //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; • } • } • }

  15. 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; • } • }

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

  17. //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>

  18. //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(); • } • }

  19. 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'"); • } } • }

  20. 5、GridView实现自动编号: 原有的用户ID呢?

  21. //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>

  22. //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(); • }

  23. 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(); • } • }

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

  25. //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>

  26. //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); • } • } • }

  27. 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(); • } • }

  28. 7、GridView显示隐藏某一列

  29. //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" />

  30. //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; • }

  31. 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; • }

  32. 8、GridView固定表头

  33. //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>

  34. //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>

  35. //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(); • } • }

More Related