我在代码隐藏中向gridview添加了一列,如下所示:
field = new BoundField();
field.HeaderText = "Phone Number";
field.DataField = "PhoneNumber";
field.HtmlEncode = false;
field.DataFormatString = "{0:###-###-####}";
gridView.Columns.Add(field);
但是,DataFormatString无法正常工作,因为PhoneNumber字段是字符串,而不是数字。无论如何要在DataFormatString中处理它,或者在我到达这一点之前是否需要转换数据类型?
答案 0 :(得分:1)
这很棘手。我可能会在网格的RowDataBound事件中“手动”格式化它。
答案 1 :(得分:0)
首选是在SQL
中完成 select cast(phone as int) as Phone,...
如果没有,请将该列设为模板化列,然后您将具有以下内容:
<asp:TextBox ID="TextBox1" runat="server"
Text='<%#(DataBinder.Eval(Container.DataItem, "Phone")== System.DBNull.Value)?
"":
String.Format("{0:(###) ###-####}",
Convert.ToInt64(DataBinder.Eval(Container.DataItem, "Phone"))))
%>'>
</asp:TextBox>