9.GridView实现自动编号:
效果图:
实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2
}
2
注意这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
1
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
2
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">
3
<FooterStyle BackColor="White" ForeColor="#000066" />
4
<Columns>
5
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
6
<asp:BoundField DataField="姓名" HeaderText="用户姓名" />
7
<asp:BoundField DataField="员工性别" HeaderText="性别" />
8
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
9
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
10
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
11
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
12
</Columns>
13
<RowStyle ForeColor="#000066" />
14
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
15
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
16
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
17
</asp:GridView>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
10.GridView实现自定义时间货币等字符串格式:
效果图:
图1-未格式化前
图2-格式化后
解决方法:
在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的
在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的
1 <asp :BoundField DataField="CreationDate"
2 DataFormatString="{0:M-dd-yyyy}"
3 HeaderText="CreationDate" />
2 DataFormatString="{0:M-dd-yyyy}"
3 HeaderText="CreationDate" />
主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决
1、
1
<asp:GridView ID="GridView1" runat="server">
2
<columns>
3
<asp :BoundField DataField="CreationDate"
4
DataFormatString="{0:M-dd-yyyy}"
5
HtmlEncode="false"
6
HeaderText="CreationDate" />
7
</columns>
8
</asp:GridView>
2
3
4
5
6
7
8
将htmlencode设置为false即可
另外的解决方法为,使用模版列
1
<asp:GridView ID="GridView3" runat="server" >
2
<columns>
3
<asp:TemplateField HeaderText="CreationDate" >
4
<itemtemplate>
5
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>' />
6
</itemtemplate>
7
<edititemtemplate>
8
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>' />
9
</edititemtemplate>
10
</asp:TemplateField>
11
</columns>
12
</asp:GridView>
2
3
4
5
6
7
8
9
10
11
12
前台代码:
1
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="身份证号码"
2
DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">
3
<Columns>
4
<asp:BoundField DataField="身份证号码" HeaderText="身份证号码" ReadOnly="True" SortExpression="身份证号码" />
5
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
6
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
7
<asp:BoundField DataField="出生日期" HeaderText="出生日期" SortExpression="出生日期" />
8
<asp:BoundField DataField="起薪" HeaderText="起薪" SortExpression="起薪" />
9
</Columns>
10
<FooterStyle BackColor="White" ForeColor="#000066" />
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
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:北风贸易ConnectionString1 %>"
17
SelectCommand="SELECT top 5 [出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] FROM [飞狐工作室]" DataSourceMode="DataReader"></asp:SqlDataSource>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
附录-常用格式化公式:
{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
{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实现用“...”代替超长字符串:
效果图:
解决方法:数据绑定后过滤每一行即可
1
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
2
}
2
调用的方法:
1
public string SubStr(string sString, int nLeng)
2
}
2
后台全部代码:
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
public partial class _Default : System.Web.UI.Page
12
}
2
3
4
5
6
7
8
9
10
11
12
12.GridView一般换行与强制换行:
效果图:
首先设置<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" ItemStyle-Width="100" />
gridview里有一列绑定的数据很长,显示的时候在一行里面显示,页面拉得很宽。
原因是连续英文段为一个整体导致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。
gridview里有一列绑定的数据很长,显示的时候在一行里面显示,页面拉得很宽。
原因是连续英文段为一个整体导致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。
如果要给所有的列增加此属性:
1
protected void Page_Load(object sender, EventArgs e)
2
}
2
总之:善用CSS的word-break:break-all;word-wrap:break-word属性即可,这个属性是通用的对于顽固的南换行问题都可以解决,不局限于GridView。
13.GridView显示隐藏某一列:
本方案为月儿独创,不同于网上其他方式,我觉得用一个CheckBox更人性化,这样可以隐藏不必要的列,让用户自己选择需要出现的列,在处理多列时这是一个很好的解决方案!
本方案为月儿独创,不同于网上其他方式,我觉得用一个CheckBox更人性化,这样可以隐藏不必要的列,让用户自己选择需要出现的列,在处理多列时这是一个很好的解决方案!
效果图:
图1-开始
图2-点击显示的CheckBox后
解决方案:
1
public void bind()
2
}
2
双击CheckBox1,在CheckedChanged方法里写上代码,最后代码如下:
1
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
2
}
2
注意:CheckBox1的AutoPostBack要True!
后台全部代码如下:
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
public partial class _Default : System.Web.UI.Page
12
}
2
3
4
5
6
7
8
9
10
11
12
前台代码:
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml" >
3
<head runat="server">
4
<title>GridView显示隐藏列 清清月儿http://blog.csdn.net/21aspnet </title>
5
</head>
6
<body style="font-size=12px">
7
<form id="form1" runat="server">
8
<div>
9
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
10
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
11
<FooterStyle BackColor="White" ForeColor="#000066" />
12
<Columns>
13
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
14
<asp:BoundField DataField="姓名" HeaderText="用户姓名" />
15
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
16
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
17
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
18
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
19
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
20
</Columns>
21
<RowStyle ForeColor="#000066" />
22
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
23
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
24
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
25
</asp:GridView>
26
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Font-Size="12px"
27
OnCheckedChanged="CheckBox1_CheckedChanged" Text="显示隐藏家庭住址" /></div>
28
</form>
29
</body>
30
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30