格式化对象ToString

时间:2014-01-22 12:53:18

标签: c# asp.net .net string-formatting

如何在此上下文中格式化两个字符串数据 Somma

 SqlConnection con = new SqlConnection(strConnString);
    con.Open();
    str = "select * from Pagamenti ORDER BY [Data] DESC";
    com = new SqlCommand(str, con);
    sqlda = new SqlDataAdapter(com);
    ds = new DataSet();
    sqlda.Fill(ds, "Pagamenti");

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        DropDownList1.Items.Add(ds.Tables[0].Rows[i]["Id"] +
            "   --|--   " + ds.Tables[0].Rows[i]["Data"].ToString() +
            "   --|--   " + ds.Tables[0].Rows[i]["Somma"]);
    }
    con.Close();

ToString()没有采取任何措施,我需要分别“dd / MM / yyyy”和“R#。###”。

2 个答案:

答案 0 :(得分:10)

我想这是因为返回值的类型是object,它确实没有参数。

尝试将对象转换为正确的类型并再次调用ToString

像这样:

Convert.ToDateTime(ds.Tables[0].Rows[i]["Data"]).ToString("dd/MM/yyyy")

或让string.Format处理它:

string.Format("{0:dd/MM/yyyy}", ds.Tables[0].Rows[i]["Data"])

答案 1 :(得分:0)

string.Format("{0} --|-- {1} --|-- {2}",ds.Tables[0].Rows[i]["Id"].ToString(),ds.Tables[0].Rows[i]["Data"].ToString(),ds.Tables[0].Rows[i]["Somma"].ToString());
相关问题