本来呢我没想过要把这个东西转过来,可是有一天我竟然发现清清月儿的空间全空了,什么也看不到了,所以找呀找呀,终于找到了别人转过的清清月儿的东西,所以我也转过来了,就算是保存一下,权做自个儿学习了。
快速预览:
GridView无代**分页排序
GridView选中,编辑,取消,删除
GridView正反双向排序
GridView和下拉菜单DropDownList结合
GridView和CheckBox结合
鼠标移到GridView某一行时改变该行的背景色方法一
鼠标移到GridView某一行时改变该行的背景色方法二
GridView实现删除时弹出确认对话框
GridView实现自动编号
GridView实现自定义时间货币等字符串格式
GridView实现用“...”代替超长字符串
GridView一般换行与强制换行
GridView显示隐藏某一列
GridView弹出新页面/弹出新窗口
GridView固定表头(不用javascript只用CSS,2行代**,很好用)
GridView合并表头多重表头无错完美版(以合并3列3行举例)
GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
GridView加入自动求和求平均值小计
GridView数据导入Excel/Excel数据读入GridView
1.GridView无代**分页排序:
效果图:
1.AllowSorting设为True,aspx代**中是AllowSorting="True";
2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代**中是PageSize="12"。
3.默认的是单向排序的,右击GridView弹出“属**”,选择AllowSorting为True即可。
2.GridView选中,编辑,取消,删除:
效果图:
后台代**:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class _Default : System.Web.UI.Page
13{
14
15//清清月儿http://blog.csdn.net/21aspnet
16 SqlConnection sqlcon;
17 SqlCommand sqlcom;
18 string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密**";
19 protected void Page_Load(object sender, EventArgs e)
20 {
21 if (!IsPostBack)
22 {
23 bind();
24 }
25 }
26 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
27 {
28 GridView1.EditIndex = e.NewEditIndex;
29 bind();
30 }
31
32//删除
33 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
34 {
35 string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
36 sqlcon = new SqlConnection(strCon);
37 sqlcom = new SqlCommand(sqlstr,sqlcon);
38 sqlcon.Open();
39 sqlcom.ExecuteNonQuery();
40 sqlcon.Close();
41 bind();
42 }
43
44//更新
45 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
46 {
47 sqlcon = new SqlConnection(strCon);
48 string sqlstr = "update 表 set 字段1='"
49 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
50 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
51 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
52 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
53 sqlcom=new SqlCommand(sqlstr,sqlcon);
54 sqlcon.Open();
55 sqlcom.ExecuteNonQuery();
56 sqlcon.Close();
57 GridView1.EditIndex = -1;
58 bind();
59 }
60
61//取消
62 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
63 {
64 GridView1.EditIndex = -1;
65 bind();
66 }
67
68//绑定
69 public void bind()
70 {
71 string sqlstr = "select * from 表";
72 sqlcon = new SqlConnection(strCon);
73 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
74 DataSet myds = new DataSet();
75 sqlcon.Open();
76 myda.Fill(myds, "表");
77 GridView1.DataSource = myds;
78 GridView1.DataKeyNames = new string[] { "id" };//主键
79 GridView1.DataBind();
80 sqlcon.Close();
81 }
82}
83
前台主要代**:
1<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
2 ForeColor="#333333" GridLines="None" nRowDeleting="GridView1_RowDeleting" nRowEditing="GridView1_RowEditing"
3 nRowUpdating="GridView1_RowUpdating" nRowCancelingEdit="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>
3.GridView正反双向排序:
效果图:点姓名各2次的排序,点其他也一样可以。
后台代**:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12public partial class Default3 : System.Web.UI.Page
13{
14
15//清清月儿的博客http://blog.csdn.net/21aspnet
16 SqlConnection sqlcon;
17 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 if (!IsPostBack)
21 {
22 ViewState["SortOrder"] = "身份证号**";
23 ViewState["OrderDire"] = "ASC";
24 bind();
25 }
26 }
27 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
28 {
29 string sPage = e.SortExpression;
30 if (ViewState["SortOrder"].ToString() == sPage)
31 {
32 if (ViewState["OrderDire"].ToString() == "Desc")
33 ViewState["OrderDire"] = "ASC";
34 else
35 ViewState["OrderDire"] = "Desc";
36 }
37 else
38 {
39 ViewState["SortOrder"] = e.SortExpression;
40 }
41 bind();
42 }
43
44 public void bind()
45 {
46
47 string sqlstr = "select top 5 * from 飞狐工作室";
48 sqlcon = new SqlConnection(strCon);
49 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
50 DataSet myds = new DataSet();
51 sqlcon.Open();
52 myda.Fill(myds, "飞狐工作室");
53 DataView view = myds.Tables["飞狐工作室"].DefaultView;
54 string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
55 view.Sort = sort;
56 GridView1.DataSource = view;
57 GridView1.DataBind();
58 sqlcon.Close();
59 }
60}
61
62
前台主要代**:
1<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
2 CellPadding="3" Font-Size="9pt" nSorting="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
17
18
19
4.GridView和下拉菜单DropDownList结合:
效果图:
后台代**:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12public 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>
21
5.GridView和CheckBox结合:
效果图:
后台代**:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class Default5 : System.Web.UI.Page
13{
14//清清月儿http://blog.csdn.net/21aspnet
15 SqlConnection sqlcon;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24 protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
25 {
26 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
27 {
28 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
29 if (CheckBox2.Checked == true)
30 {
31 cbox.Checked = true;
32 }
33 else
34 {
35 cbox.Checked = false;
36 }
37 }
38 }
39 protected void Button2_Click(object sender, EventArgs e)
40 {
41 sqlcon = new SqlConnection(strCon);
42 SqlCommand sqlcom;
43 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
44 {
45 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
46 if (cbox.Checked == true)
47 {
48
49 string sqlstr = "delete from 飞狐工作室 where 身份证号**='" + GridView1.DataKeys[i].Value + "'";
50 sqlcom = new SqlCommand(sqlstr, sqlcon);
51 sqlcon.Open();
52 sqlcom.ExecuteNonQuery();
53 sqlcon.Close();
54 }
55 }
56 bind();
57 }
58 protected void Button1_Click(object sender, EventArgs e)
59 {
60 CheckBox2.Checked = false;
61 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
62 {
63 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
64 cbox.Checked = false;
65 }
66 }
67 public void bind()
68 {
69 string sqlstr = "select top 5 * from 飞狐工作室";
70 sqlcon = new SqlConnection(strCon);
71 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
72 DataSet myds = new DataSet();
73 sqlcon.Open();
74 myda.Fill(myds, "tb_Member");
75 GridView1.DataSource = myds;
76 GridView1.DataKeyNames = new string[] { "身份证号**" };
77 GridView1.DataBind();
78 sqlcon.Close();
79 }
80}
81
82//前台主要代**:
83<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
84 CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
85 <FooterStyle. BackColor="White" ForeColor="#000066" />
86 <Columns>
87 <asp:TemplateField>
88 <ItemTemplate>
89 <asp:CheckBox ID="CheckBox1" runat="server" />
90 </ItemTemplate>
91 </asp:TemplateField>
92 <asp:BoundField DataField="身份证号**" HeaderText="用户ID" SortExpression="身份证号**" />
93 <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
94
95 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
96
97 </Columns>
98 <RowStyle. ForeColor="#000066" />
99 <SelectedRowStyle. BackColor="#669999" Font-Bold="True" ForeColor="White" />
100 <PagerStyle. BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
101 <HeaderStyle. BackColor="#006699" Font-Bold="True" ForeColor="White" />
102 </asp:GridView>
103 <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" nCheckedChanged="CheckBox2_CheckedChanged"
104 Text="全选" />
105 <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" nClick="Button1_Click" />
106 <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" nClick="Button2_Click" />
107
108
6.鼠标移到GridView某一行时改变该行的背景色方法一:
效果图:
做法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代**,最后代**如下所示:
1protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 int i;
4 //执行循环,保证每条数据都可以更新
5 for (i = 0; i < GridView1.Rows.Count; i++)
6 {
7 //首先判断是否是数据行
8 if (e.Row.RowType == DataControlRowType.DataRow)
9 {
10 //当鼠标停留时更改背景色
11 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
12 //当鼠标移开时还原背景色
13 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
14 }
15 }
16
17 }
18
19//前台代**:
20<html xmlns="http://www.w3.org/1999/xhtml" >
21<head runat="server">
22 <title>实现鼠标划过改变GridView的行背景色 清清月儿http://blog.csdn.net/21aspnet </title>
23</head>
24<body>
25 <form. id="form1" runat="server">
26 <div>
27 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="身份证号**"
28 DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" nRowDataBound="GridView1_RowDataBound">
29 <Columns>
30 <asp:BoundField DataField="身份证号**" HeaderText="身份证号**" ReadOnly="True" SortExpression="身份证号**" />
31 <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
32 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址" />
33 <asp:BoundField DataField="邮政编**" HeaderText="邮政编**" SortExpression="邮政编**" />
34 </Columns>
35 <FooterStyle. BackColor="White" ForeColor="#000066" />
36 <RowStyle. ForeColor="#000066" />
37 <SelectedRowStyle. BackColor="#669999" Font-Bold="True" ForeColor="White" />
38 <PagerStyle. BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
39 <HeaderStyle. BackColor="#006699" Font-Bold="True" ForeColor="White" />
40 </asp:GridView>
41 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:北风贸易ConnectionString1 %>"
42 SelectCommand="SELECT top 5 [身份证号**], [姓名], [员工**别], [家庭住址], [邮政编**] FROM [飞狐工作室]" DataSourceMode="DataReader"></asp:SqlDataSource>
43
44 </div>
45 </form>
46</body>
47</html>
48
49
7.鼠标移到GridView某一行时改变该行的背景色方法二:
效果图:
做法:和上面的一样就是代**不同
1protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //int i;
4 /**/////执行循环,保证每条数据都可以更新
5 //for (i = 0; i < GridView1.Rows.Count; i++)
6 //{
7 // //首先判断是否是数据行
8 // if (e.Row.RowType == DataControlRowType.DataRow)
9 // {
10 // //当鼠标停留时更改背景色
11 // e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
12 // //当鼠标移开时还原背景色
13 // e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
14 // }
15 //}
16 //如果是绑定数据行
17 if (e.Row.RowType == DataControlRowType.DataRow)
18 {
19 //鼠标经过时,行背景色变
20 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
21 //鼠标移出时,行背景色变
22 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
23 }
24
25 }
26
27
8.GridView实现删除时弹出确认对话框:
效果图:
实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代**,最后代**如下所示:
1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //如果是绑定数据行
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
7 {
8 ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript.:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
9 }
10 }
11
12 }
13
14
9.GridView实现自动编号:
效果图:
实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代**,最后代**如下所
1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //如果是绑定数据行 //清清月儿http://blog.csdn.net/21aspnet
4 if (e.Row.RowType == DataControlRowType.DataRow)
5 {
6 /**/////鼠标经过时,行背景色变
7 //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
8 /**/////鼠标移出时,行背景色变
9 //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
10
11 /**/////当有编辑列时,避免出错,要加的RowState判断
12 //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
13 //{
14 // ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript.:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
15 //}
16
17 }
18 if (e.Row.RowIndex != -1)
19 {
20 int id = e.Row.RowIndex + 1;
21 e.Row.Cells[0].Text = id.ToString();
22 }
23
24 }
25
26
27
28//注意这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
29<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" nRowDeleting="GridView1_RowDeleting" nRowEditing="GridView1_RowEditing"
30 nRowUpdating="GridView1_RowUpdating" nRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" nRowDataBound="GridView1_RowDataBound">
31 <FooterStyle. BackColor="White" ForeColor="#000066" />
32 <Columns>
33 <asp:BoundField DataField="身份证号**" HeaderText="编号" ReadOnly="True" />
34 <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
35 <asp:BoundField DataField="员工**别" HeaderText="**别" />
36 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
37 <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
38 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
39 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
40 </Columns>
41 <RowStyle. ForeColor="#000066" />
42 <SelectedRowStyle. BackColor="#669999" Font-Bold="True" ForeColor="White" />
43 <PagerStyle. BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
44 <HeaderStyle. BackColor="#006699" Font-Bold="True" ForeColor="White" />
45 </asp:GridView>
46
47
10.GridView实现自定义时间货币等字符串格式:
效果图:
图1-未格式化前
图2-格式化后
解决方法:
在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的
<asp :BoundField DataField='CreationDate'
DataFormatString='{0:M-dd-yyyy}'
HeaderText='CreationDate' />
主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决
1//1
2<asp :GridView ID='GridView1' runat='server'>
3<columns>
4<asp :BoundField DataField='CreationDate'
5DataFormatString='{0:M-dd-yyyy}'
6HtmlEncode='false'
7HeaderText='CreationDate' />
8</columns>
9</asp>
10
11//将htmlencode设置为false即可
12
13//另外的解决方法为,使用模版列
14
15<asp :GridView ID='GridView3' runat='server' >
16<columns>
17<asp :TemplateField HeaderText='CreationDate' >
18<edititemtemplate>
19<asp :Label ID='Label1' runat='server'
20Text='<%# Eval'CreationDate', '{0:M-dd-yyyy}' %>'>
21</asp>
22</edititemtemplate>
23<itemtemplate>
24<asp :Label ID='Label1' runat='server'
25Text=’<%# Bind'CreationDate', '{0:M-dd-yyyy}' %>'>
26</asp>
27</itemtemplate>
28</asp>
29</columns>
30</asp>
31
32//前台代码:
33<asp:GridView ID='GridView1' runat='server' AutoGenerateColumns='False' DataKeyNames='身份证号码'
34 DataSourceID='SqlDataSource1' AllowSorting='True' BackColor='White' BorderColor='#CCCCCC' BorderStyle='None' BorderWidth='1px' CellPadding='3' Font-Size='12px' ōnRowDataBound='GridView1_RowDataBound'>
35 <Columns>
36 <asp:BoundField DataField='身份证号码' HeaderText='身份证号码' ReadOnly='True' SortExpression='身份证号码' />
37 <asp:BoundField DataField='姓名' HeaderText='姓名' SortExpression='姓名' />
38 <asp:BoundField DataField='邮政编码' HeaderText='邮政编码' SortExpression='邮政编码' />
39 <asp:BoundField DataField='出生日期' HeaderText='出生日期' SortExpression='出生日期' />
40 <asp:BoundField DataField='起薪' HeaderText='起薪' SortExpression='起薪' />
41 </Columns>
42 <FooterStyle BackColor='White' ForeColor='#000066' />
43 <RowStyle ForeColor='#000066' />
44 <SelectedRowStyle BackColor='#669999' Font-Bold='True' ForeColor='White' />
45 <PagerStyle BackColor='White' ForeColor='#000066' HorizontalAlign='Left' />
46 <HeaderStyle BackColor='#006699' Font-Bold='True' ForeColor='White' />
47 </asp:GridView>
48 <asp:SqlDataSource ID='SqlDataSource1' runat='server' ConnectionString='<%$ ConnectionStrings:北风贸易ConnectionString1 %>'
49 SelectCommand='SELECT top 5 [出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] FROM [飞狐工作室]' DataSourceMode='DataReader'></asp:SqlDataSource>
50
51
附录-常用格式化公式:
{0:C} 货币;
{0:D4}由0填充的4个字符宽的字段中显示整数;
{0:000.0}四舍五入小数点保留第几位有效数字;
{0:N2}小数点保留2位有效数字;{0:N2}% 小数点保留2位有效数字加百分号;
{0:D}长日期;{0:d}短日期;{0:yy-MM-dd} 例如07-3-25;;{0:yyyy-MM-dd} 例如2007-3-25
11.GridView实现用“...”代替超长字符串:
效果图:
解决方法:数据绑定后过滤每一行即可
1for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
2 {
3 DataRowView mydrv;
4 string gIntro;
5 if (GridView1.PageIndex == 0)
6 {
7 mydrv = myds.Tables["飞狐工作室"].DefaultView[i];//表名
8 gIntro = Convert.ToString(mydrv["家庭住址"]);//所要处理的字段
9 GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
10 }
11 else
12 {
13 mydrv = myds.Tables["飞狐工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
14 gIntro = Convert.ToString(mydrv["家庭住址"]);
15 GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
16 }
17 }
18//调用的方法:
19
20 public string SubStr(string sString, int nLeng)
21 {
22 if (sString.Length <= nLeng)
23 {
24 return sString;
25 }
26 string sNewStr = sString.Substring(0, nLeng);
27 sNewStr = sNewStr + "";
28 return sNewStr;
29 }
30
31//后台全部代码:
32using System;
33using System.Data;
34using System.Configuration;
35using System.Web;
36using System.Web.Security;
37using System.Web.UI;
38using System.Web.UI.WebControls;
39using System.Web.UI.WebControls.WebParts;
40using System.Web.UI.HtmlControls;
41using System.Data.SqlClient;
42
43public partial class _Default : System.Web.UI.Page
44{
45 SqlConnection sqlcon;
46 SqlCommand sqlcom;
47 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
48 protected void Page_Load(object sender, EventArgs e)
49 {
50 if (!IsPostBack)
51 {
52 ViewState["SortOrder"] = "身份证号码";
53 ViewState["OrderDire"] = "ASC";
54 bind();
55 }
56 }
57 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
58 {
59 GridView1.EditIndex = e.NewEditIndex;
60 bind();
61 }
62 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
63 {
64 string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
65 sqlcon = new SqlConnection(strCon);
66 sqlcom = new SqlCommand(sqlstr,sqlcon);
67 sqlcon.Open();
68 sqlcom.ExecuteNonQuery();
69 sqlcon.Close();
70 bind();
71 }
72 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
73 {
74 sqlcon = new SqlConnection(strCon);
75 string sqlstr = "update 飞狐工作室 set 姓名='"
76 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
77 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
78 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
79 sqlcom=new SqlCommand(sqlstr,sqlcon);
80 sqlcon.Open();
81 sqlcom.ExecuteNonQuery();
82 sqlcon.Close();
83 GridView1.EditIndex = -1;
84 bind();
85 }
86 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
87 {
88 GridView1.EditIndex = -1;
89 bind();
90 }
91 public void bind()
92 {
93 string sqlstr = "select top 5 * from 飞狐工作室";
94 sqlcon = new SqlConnection(strCon);
95 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
96 DataSet myds = new DataSet();
97 sqlcon.Open();
98 myda.Fill(myds, "飞狐工作室");
99 GridView1.DataSource = myds;
100 GridView1.DataKeyNames = new string[] { "身份证号码" };
101 GridView1.DataBind();
102 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
103 {
104 DataRowView mydrv;
105 string gIntro;
106 if (GridView1.PageIndex == 0)
107 {
108 mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
109 gIntro = Convert.ToString(mydrv["家庭住址"]);
110 GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
111 }
112 else
113 {
114 mydrv = myds.Tables["飞狐工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
115 gIntro = Convert.ToString(mydrv["家庭住址"]);
116 GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
117 }
118 }
119
120 sqlcon.Close();
121 }
122 public string SubStr(string sString, int nLeng)
123 {
124 if (sString.Length <= nLeng)
125 {
126 return sString;
127 }
128 string sNewStr = sString.Substring(0, nLeng);
129 sNewStr = sNewStr + "";
130 return sNewStr;
131 }
132 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
133 {
134 //如果是绑定数据行
135 if (e.Row.RowType == DataControlRowType.DataRow)
136 {
137 /**/////鼠标经过时,行背景色变
138 //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
139 /**/////鼠标移出时,行背景色变
140 //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
141
142 /**/////当有编辑列时,避免出错,要加的RowState判断
143 //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
144 //{
145 // ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
146 //}
147
148 }
149 if (e.Row.RowIndex != -1)
150 {
151 int id = e.Row.RowIndex + 1;
152 e.Row.Cells[0].Text = id.ToString();
153 }
154
155 }
156}
157