<DIV id=blogDetailDiv style="FONT-SIZE: 16px">GridView实现自定义时间货币等字符串格式 <BR> 在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的 <BR><BR><asp :BoundField DataField="CreationDate" <BR>DataFormatString="{0:M-dd-yyyy}" <BR>HeaderText="CreationDate" /> <BR><BR>主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决 <BR><BR>1、 <BR><BR><asp :GridView ID="GridView1" runat="server"> <BR><columns> <BR><asp :BoundField DataField="CreationDate" <BR>DataFormatString="{0:M-dd-yyyy}" <BR>HtmlEncode="false" <BR>HeaderText="CreationDate" /> <BR></columns> <BR></asp> <BR><BR>将htmlencode设置为false即可 <BR><BR>另外的解决方法为,使用模版列 <BR><BR><asp :GridView ID="GridView3" runat="server" > <BR><columns> <BR><asp :TemplateField HeaderText="CreationDate" > <BR><edititemtemplate> <BR><asp :Label ID="Label1" runat="server" <BR>Text=''<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>''> <BR></asp> <BR></edititemtemplate> <BR><itemtemplate> <BR><asp :Label ID="Label1" runat="server" <BR>Text=’<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>''> <BR></asp> <BR></itemtemplate> <BR></asp> <BR></columns> <BR></asp> <BR><BR>{0:C} 货币; <BR>{0:D4}由0填充的4个字符宽的字段中显示整数; <BR>{0:000.0}四舍五入小数点保留第几位有效数字; <BR>{0:N2}小数点保留2位有效数字;{0:N2}% 小数点保留2位有效数字加百分号; <BR>{0:D}长日期;{0:d}短日期;{0:yy-MM-dd} 例如07-3-25;;{0:yyyy-MM-dd} 例如2007-3-25 <BR>GridView一般换行与强制换行 <BR> 设置<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" ItemStyle-Width="100" /> <BR>gridview里有一列绑定的数据很长,显示的时候在一行里面显示,页面拉得很宽。 <BR>原因是连续英文段为一个整体导致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。 <BR><BR>如果要给所有的列增加此属性: <BR>protected void Page_Load(object sender, EventArgs e) <BR> { <BR> //正常换行 <BR> GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal"); <BR> //下面这行是自动换行 <BR> GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word"); <BR> if (!IsPostBack) <BR> { <BR> bind();//调用数据绑定即可 <BR> } <BR> } <BR><BR>GridView加入自动求和求平均值小计 <BR> private double sum = 0; <BR>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) <BR> { <BR> if (e.Row.RowIndex >= 0) <BR> { <BR> sum += Convert.ToDouble(e.Row.Cells[6].Text); <BR> } <BR> else if (e.Row.RowType == DataControlRowType.Footer) <BR> { <BR> e.Row.Cells[5].Text = "总金额为:"; <BR> e.Row.Cells[6].Text = sum.ToString(); <BR> e.Row.Cells[3].Text = "平均金额为:"; <BR> e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString(); <BR> } <BR> } <BR>前台:唯一的就是设置ShowFooter="True" ! <BR>GridView数据导入Excel/Excel数据读入GridView <BR> protected void Button1_Click(object sender, EventArgs e) <BR> { <BR> Export("application/ms-excel", "学生成绩报表.xls"); <BR> } <BR><BR> private void Export(string FileType, string FileName) <BR> { <BR> Response.Charset = "GB2312"; <BR> Response.ContentEncoding = System.Text.Encoding.UTF7; <BR> Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); <BR> Response.ContentType = FileType; <BR> this.EnableViewState = false; <BR> StringWriter tw = new StringWriter(); <BR> HtmlTextWriter hw = new HtmlTextWriter(tw); <BR> GridView1.RenderControl(hw); <BR> Response.Write(tw.ToString()); <BR> Response.End(); <BR> } <BR>//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 <BR> public override void VerifyRenderingInServerForm(Control control) <BR> { <BR> } <BR>还有由于是文件操作所以要引入名称空间IO和Text <BR><A href="http://www.bdqn-sx.com">www.bdqn-sx.com</A></DIV> |
|