ToString()格式和用法大全

C

ToString()格式和用法大全 
货币 
2.5.ToString("C") 
¥2.50 

十进制数 
25.ToString("D5") 
00025 

科学型 
25000.ToString("E") 
2.500000E+005 

固定点 
25.ToString("F2") 
25.00 

常规 
2.5.ToString("G") 
2.5 

数字 
2500000.ToString("N") 
2,500,000.00 

十六进制 
255.ToString("X") 
FF 
formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看) 必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。 
常用格式举例: 
(1) int i=12345; 
this.textBox1.Text=i.ToString(); 
//结果 12345(this指当前对象,或叫当前类的实例) 
this.textBox2.Text=i.ToString("d8"); 
//结果 00012345 
(2) int i=123; 
double j=123.45; 
string s1=string.Format("the value is {0,7:d}",i); 
string s2=string.Format("the value is {0,7:f3}",j); 
this.textBox1.Text=s1 ; 
//结果 the value is 123 
this.textBox2.Text=s2; 
//结果 the value is 123.450 
(3)double i=12345.6789; 
this.textBox1.Text=i.ToString("f2"); //结果 12345.68 
this.textBox2.Text=i.ToString("f6"); 
//结果 12345.678900 
(4)double i=12345.6789; 
this.textBox1.Text=i.ToString("n"); //结果 12,345.68 
this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789 
(5)double i=0.126; 
string s=string.Format("the value is {0:p}",i); 
this.textBox1.Text=i.ToString("p"); //结果 12.6% 
this.textBox2.Text=s; //结果 the value is 12.6% 
(6) DateTime dt =new DateTime(2003,5,25); 
this.textBox1.Text=dt.ToString("yy.M.d"); 
//结果 03.5.25 
this.textBox2.Text=dt.ToString(“yyyy年M月”); 
//结果 2003年5月 
(7) int i=123; 
double j=123.45; 
string s=string.Format("i:{0,-7},j:{1,7}",i,j); 
//-7表示左对齐,占7位 
this.textBox1.Text=s ; 
//结果i:123 ,j: 123.45 
******DataBinder.Eval用法范例*********************************************************************** 
//显示二位小数 
//<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>   
//{0:G}代表显示True或False 
<%# DataBinder.Eval(Container.DataItem, "Discontinued", "{0:G}") %> 
//转换类型 
((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4) 
{0:d} 日期只显示年月日 
{0:yyyy-mm-dd} 按格式显示年月日 
{0:c} 货币样式 
<%#DataBinder.Eval(Container.DataItem,"h_yk", "${0:F2}") %>美元 
如何设定全局变量
Global.asax中Application_Start()事件中添加Application[属性名] = xxx;就是你的全局变量 
添加一个编号列: 
DataTable dt= c.ExecuteRtnTableForAccess(sqltxt); //执行sql返回的 
DataTable DataColumn dc=dt.Columns.Add("number",System.Type.GetType("System.String")); 
for(int i=0;i<dt.Rows.Count;i++) 

dt.Rows ["number"]=(i+1).ToString();
}
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
DataGrid1中添加一个CheckBox,页面中添加一个全选框
private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
{
foreach(DataGridItem thisitem in DataGrid1.Items)
{
((CheckBox)thisitem.Cells[0].Controls[1]).Checked=CheckBox2.Checked;
}
}
获取错误信息并到指定页面
不要使用Response.Redirect,而应该使用Server.Transfer
// 在 global.asax 中
    protected void Application_Error(Object sender, EventArgs e) 
    {
    if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");
//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
}
Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理
原文地址:https://www.cnblogs.com/ruiati/p/2863917.html