本期内容:
GridView无代码分页排序
GridView选中,编辑,取消,删除
GridView正反双向排序
GridView和下拉菜单DropDownList结合
GridView选中,编辑,取消,删除
GridView正反双向排序
GridView和下拉菜单DropDownList结合
1.GridView无代码分页排序:
效果图:
1.AllowSorting设为True,aspx代码中是AllowSorting="True";
2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。
3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。
2.GridView选中,编辑,取消,删除:
效果图:
后台代码:
你可以使用sqlhelper,本文没用。代码如下:
你可以使用sqlhelper,本文没用。代码如下:
1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11
12 public partial class _Default : System.Web.UI.Page
13 {
14 //清清月儿http://blog.csdn.net/21aspnet
15 SqlConnection sqlcon;
16 SqlCommand sqlcom;
17 string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 if (!IsPostBack)
21 {
22 bind();
23 }
24 }
25 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
26 {
27 GridView1.EditIndex = e.NewEditIndex;
28 bind();
29 }
30
31 //删除
32 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
33 {
34 string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
35 sqlcon = new SqlConnection(strCon);
36 sqlcom = new SqlCommand(sqlstr,sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 bind();
41 }
42
43 //更新
44 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
45 {
46 sqlcon = new SqlConnection(strCon);
47 string sqlstr = "update 表 set 字段1='"
48 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
49 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
50 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
51 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
52 sqlcom=new SqlCommand(sqlstr,sqlcon);
53 sqlcon.Open();
54 sqlcom.ExecuteNonQuery();
55 sqlcon.Close();
56 GridView1.EditIndex = -1;
57 bind();
58 }
59
60 //取消
61 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
62 {
63 GridView1.EditIndex = -1;
64 bind();
65 }
66
67 //绑定
68 public void bind()
69 {
70 string sqlstr = "select * from 表";
71 sqlcon = new SqlConnection(strCon);
72 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
73 DataSet myds = new DataSet();
74 sqlcon.Open();
75 myda.Fill(myds, "表");
76 GridView1.DataSource = myds;
77 GridView1.DataKeyNames = new string[] { "id" };//主键
78 GridView1.DataBind();
79 sqlcon.Close();
80 }
81}
82
83
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11
12 public partial class _Default : System.Web.UI.Page
13 {
14 //清清月儿http://blog.csdn.net/21aspnet
15 SqlConnection sqlcon;
16 SqlCommand sqlcom;
17 string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 if (!IsPostBack)
21 {
22 bind();
23 }
24 }
25 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
26 {
27 GridView1.EditIndex = e.NewEditIndex;
28 bind();
29 }
30
31 //删除
32 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
33 {
34 string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
35 sqlcon = new SqlConnection(strCon);
36 sqlcom = new SqlCommand(sqlstr,sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 bind();
41 }
42
43 //更新
44 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
45 {
46 sqlcon = new SqlConnection(strCon);
47 string sqlstr = "update 表 set 字段1='"
48 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
49 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
50 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
51 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
52 sqlcom=new SqlCommand(sqlstr,sqlcon);
53 sqlcon.Open();
54 sqlcom.ExecuteNonQuery();
55 sqlcon.Close();
56 GridView1.EditIndex = -1;
57 bind();
58 }
59
60 //取消
61 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
62 {
63 GridView1.EditIndex = -1;
64 bind();
65 }
66
67 //绑定
68 public void bind()
69 {
70 string sqlstr = "select * from 表";
71 sqlcon = new SqlConnection(strCon);
72 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
73 DataSet myds = new DataSet();
74 sqlcon.Open();
75 myda.Fill(myds, "表");
76 GridView1.DataSource = myds;
77 GridView1.DataKeyNames = new string[] { "id" };//主键
78 GridView1.DataBind();
79 sqlcon.Close();
80 }
81}
82
83
前台主要代码:
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
CellPadding
="4"
2 ForeColor ="#333333" GridLines ="None" OnRowDeleting ="GridView1_RowDeleting" OnRowEditing ="GridView1_RowEditing"
3 OnRowUpdating ="GridView1_RowUpdating" OnRowCancelingEdit ="GridView1_RowCancelingEdit" >
4 < FooterStyle BackColor ="#990000" Font-Bold ="True" ForeColor ="White" />
5 < Columns >
6 < asp:BoundField DataField ="身份证号码" HeaderText ="用户ID" ReadOnly ="True" />
7 < asp:BoundField DataField ="姓名" HeaderText ="用户姓名" />
8 < asp:BoundField DataField ="员工性别" HeaderText ="性别" />
9 < asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" />
10 < asp:CommandField HeaderText ="选择" ShowSelectButton ="True" />
11 < asp:CommandField HeaderText ="编辑" ShowEditButton ="True" />
12 < asp:CommandField HeaderText ="删除" ShowDeleteButton ="True" />
13 </ Columns >
14 < RowStyle ForeColor ="#000066" />
15 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
16 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
17 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
18 </ asp:GridView >
19
2 ForeColor ="#333333" GridLines ="None" OnRowDeleting ="GridView1_RowDeleting" OnRowEditing ="GridView1_RowEditing"
3 OnRowUpdating ="GridView1_RowUpdating" OnRowCancelingEdit ="GridView1_RowCancelingEdit" >
4 < FooterStyle BackColor ="#990000" Font-Bold ="True" ForeColor ="White" />
5 < Columns >
6 < asp:BoundField DataField ="身份证号码" HeaderText ="用户ID" ReadOnly ="True" />
7 < asp:BoundField DataField ="姓名" HeaderText ="用户姓名" />
8 < asp:BoundField DataField ="员工性别" HeaderText ="性别" />
9 < asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" />
10 < asp:CommandField HeaderText ="选择" ShowSelectButton ="True" />
11 < asp:CommandField HeaderText ="编辑" ShowEditButton ="True" />
12 < asp:CommandField HeaderText ="删除" ShowDeleteButton ="True" />
13 </ Columns >
14 < RowStyle ForeColor ="#000066" />
15 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
16 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
17 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
18 </ asp:GridView >
19
3.GridView正反双向排序:
效果图:点姓名各2次的排序,点其他也一样可以。
效果图:点姓名各2次的排序,点其他也一样可以。
后台代码:
1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12 public partial class Default3 : System.Web.UI.Page
13 {
14 //清清月儿的博客http://blog.csdn.net/21aspnet
15 SqlConnection sqlcon;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 ViewState["SortOrder"] = "身份证号码";
22 ViewState["OrderDire"] = "ASC";
23 bind();
24 }
25 }
26 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
27 {
28 string sPage = e.SortExpression;
29 if (ViewState["SortOrder"].ToString() == sPage)
30 {
31 if (ViewState["OrderDire"].ToString() == "Desc")
32 ViewState["OrderDire"] = "ASC";
33 else
34 ViewState["OrderDire"] = "Desc";
35 }
36 else
37 {
38 ViewState["SortOrder"] = e.SortExpression;
39 }
40 bind();
41 }
42
43 public void bind()
44 {
45
46 string sqlstr = "select top 5 * from 飞狐工作室";
47 sqlcon = new SqlConnection(strCon);
48 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
49 DataSet myds = new DataSet();
50 sqlcon.Open();
51 myda.Fill(myds, "飞狐工作室");
52 DataView view = myds.Tables["飞狐工作室"].DefaultView;
53 string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
54 view.Sort = sort;
55 GridView1.DataSource = view;
56 GridView1.DataBind();
57 sqlcon.Close();
58 }
59}
60
61
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12 public partial class Default3 : System.Web.UI.Page
13 {
14 //清清月儿的博客http://blog.csdn.net/21aspnet
15 SqlConnection sqlcon;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 ViewState["SortOrder"] = "身份证号码";
22 ViewState["OrderDire"] = "ASC";
23 bind();
24 }
25 }
26 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
27 {
28 string sPage = e.SortExpression;
29 if (ViewState["SortOrder"].ToString() == sPage)
30 {
31 if (ViewState["OrderDire"].ToString() == "Desc")
32 ViewState["OrderDire"] = "ASC";
33 else
34 ViewState["OrderDire"] = "Desc";
35 }
36 else
37 {
38 ViewState["SortOrder"] = e.SortExpression;
39 }
40 bind();
41 }
42
43 public void bind()
44 {
45
46 string sqlstr = "select top 5 * from 飞狐工作室";
47 sqlcon = new SqlConnection(strCon);
48 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
49 DataSet myds = new DataSet();
50 sqlcon.Open();
51 myda.Fill(myds, "飞狐工作室");
52 DataView view = myds.Tables["飞狐工作室"].DefaultView;
53 string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
54 view.Sort = sort;
55 GridView1.DataSource = view;
56 GridView1.DataBind();
57 sqlcon.Close();
58 }
59}
60
61
前台主要代码:
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AllowSorting
="True"
AutoGenerateColumns
="False"
2 CellPadding ="3" Font-Size ="9pt" OnSorting ="GridView1_Sorting" BackColor ="White" BorderColor ="#CCCCCC" BorderStyle ="None" BorderWidth ="1px" >
3 < FooterStyle BackColor ="White" ForeColor ="#000066" />
4 < Columns >
5 < asp:BoundField DataField ="身份证号码" HeaderText ="用户ID" SortExpression ="身份证号码" />
6 < asp:BoundField DataField ="姓名" HeaderText ="用户姓名" SortExpression ="姓名" />
7 < asp:BoundField DataField ="员工性别" HeaderText ="性别" SortExpression ="员工性别" />
8 < asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" SortExpression ="家庭住址" />
9
10 </ Columns >
11 < RowStyle ForeColor ="#000066" />
12 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
13 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
14 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
15 </ asp:GridView >
16
后台代码:
2 CellPadding ="3" Font-Size ="9pt" OnSorting ="GridView1_Sorting" BackColor ="White" BorderColor ="#CCCCCC" BorderStyle ="None" BorderWidth ="1px" >
3 < FooterStyle BackColor ="White" ForeColor ="#000066" />
4 < Columns >
5 < asp:BoundField DataField ="身份证号码" HeaderText ="用户ID" SortExpression ="身份证号码" />
6 < asp:BoundField DataField ="姓名" HeaderText ="用户姓名" SortExpression ="姓名" />
7 < asp:BoundField DataField ="员工性别" HeaderText ="性别" SortExpression ="员工性别" />
8 < asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" SortExpression ="家庭住址" />
9
10 </ Columns >
11 < RowStyle ForeColor ="#000066" />
12 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
13 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
14 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
15 </ asp:GridView >
16
4.GridView和下拉菜单DropDownList结合:
效果图:
后台代码:
1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12 public partial class Default4 : System.Web.UI.Page
13 {
14 SqlConnection sqlcon;
15 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 DropDownList ddl;
19 if (!IsPostBack)
20 {
21 string sqlstr = "select top 5 * from 飞狐工作室";
22 sqlcon = new SqlConnection(strCon);
23 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
24 DataSet myds = new DataSet();
25 sqlcon.Open();
26 myda.Fill(myds, "飞狐工作室");
27 GridView1.DataSource = myds;
28 GridView1.DataBind();
29 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
30 {
31 DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
32 if (Convert.ToString(mydrv["员工性别"]).Trim() == "True")
33 {
34 ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
35 ddl.SelectedIndex = 0;
36 }
37 if (Convert.ToString(mydrv["员工性别"]).Trim() == "False")
38 {
39 ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
40 ddl.SelectedIndex = 1;
41 }
42 }
43 sqlcon.Close();
44 }
45 }
46 public SqlDataReader ddlbind()
47 {
48 string sqlstr = "select distinct 员工性别 from 飞狐工作室";
49 sqlcon = new SqlConnection(strCon);
50 SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
51 sqlcon.Open();
52 return sqlcom.ExecuteReader();
53 }
54}
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12 public partial class Default4 : System.Web.UI.Page
13 {
14 SqlConnection sqlcon;
15 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 DropDownList ddl;
19 if (!IsPostBack)
20 {
21 string sqlstr = "select top 5 * from 飞狐工作室";
22 sqlcon = new SqlConnection(strCon);
23 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
24 DataSet myds = new DataSet();
25 sqlcon.Open();
26 myda.Fill(myds, "飞狐工作室");
27 GridView1.DataSource = myds;
28 GridView1.DataBind();
29 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
30 {
31 DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
32 if (Convert.ToString(mydrv["员工性别"]).Trim() == "True")
33 {
34 ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
35 ddl.SelectedIndex = 0;
36 }
37 if (Convert.ToString(mydrv["员工性别"]).Trim() == "False")
38 {
39 ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
40 ddl.SelectedIndex = 1;
41 }
42 }
43 sqlcon.Close();
44 }
45 }
46 public SqlDataReader ddlbind()
47 {
48 string sqlstr = "select distinct 员工性别 from 飞狐工作室";
49 sqlcon = new SqlConnection(strCon);
50 SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
51 sqlcon.Open();
52 return sqlcom.ExecuteReader();
53 }
54}
前台主要代码:
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AllowSorting
="True"
AutoGenerateColumns
="False"
2 CellPadding ="3" Font-Size ="9pt" BackColor ="White" BorderColor ="#CCCCCC" BorderStyle ="None" BorderWidth ="1px" >
3 < FooterStyle BackColor ="White" ForeColor ="#000066" />
4 < Columns >
5 < asp:BoundField DataField ="身份证号码" HeaderText ="用户ID" SortExpression ="身份证号码" />
6 < asp:BoundField DataField ="姓名" HeaderText ="用户姓名" SortExpression ="姓名" />
7 < asp:TemplateField HeaderText ="员工性别" >
8 < ItemTemplate >
9 < asp:DropDownList ID ="DropDownList1" runat ="server" DataSource ='<%# ddlbind()% > ' DataValueField="员工性别" DataTextField="员工性别">
10 </ asp:DropDownList >
11 </ ItemTemplate >
12 </ asp:TemplateField >
13 < asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" SortExpression ="家庭住址" />
14
15 </ Columns >
16 < RowStyle ForeColor ="#000066" />
17 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
18 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
19 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
20 </ asp:GridView >
2 CellPadding ="3" Font-Size ="9pt" BackColor ="White" BorderColor ="#CCCCCC" BorderStyle ="None" BorderWidth ="1px" >
3 < FooterStyle BackColor ="White" ForeColor ="#000066" />
4 < Columns >
5 < asp:BoundField DataField ="身份证号码" HeaderText ="用户ID" SortExpression ="身份证号码" />
6 < asp:BoundField DataField ="姓名" HeaderText ="用户姓名" SortExpression ="姓名" />
7 < asp:TemplateField HeaderText ="员工性别" >
8 < ItemTemplate >
9 < asp:DropDownList ID ="DropDownList1" runat ="server" DataSource ='<%# ddlbind()% > ' DataValueField="员工性别" DataTextField="员工性别">
10 </ asp:DropDownList >
11 </ ItemTemplate >
12 </ asp:TemplateField >
13 < asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" SortExpression ="家庭住址" />
14
15 </ Columns >
16 < RowStyle ForeColor ="#000066" />
17 < SelectedRowStyle BackColor ="#669999" Font-Bold ="True" ForeColor ="White" />
18 < PagerStyle BackColor ="White" ForeColor ="#000066" HorizontalAlign ="Left" />
19 < HeaderStyle BackColor ="#006699" Font-Bold ="True" ForeColor ="White" />
20 </ asp:GridView >