我在MySQL数据库表中有一组数据,我试图使用C#在ascx模块中的DataGrid中显示这些数据。数据库表中的值作为浮点数存储。当我将数据库表中的数据显示到我的DataGrid (即如果数据库中的值为3.30,我的DataGrid显示为3.3)时,我遇到了问题。我希望将DataGrid中的值显示为小数点2位。如果数据库表显示值3.00,我希望在我的DataGrid表中也显示相同的值。
我做了相当广泛的网上搜索和这里的堆叠溢出。我确实发现了一些类似的问题,比如C# WPF DataGrid不显示SQLite 3数据库中的十进制,但这对我想要做的事情没有帮助。
下面是我的ascx和ascx.cs的代码。对于解决这一问题的任何帮助都将不胜感激。
ascx代码
<asp:DataGrid runat="server" ID="MyDataGrid2" Width="50%" BorderColor="Black" ShowFooter="false" CellPadding="4" CellSpacing="2" Font-Names="Verdana" Font-Size="10pt" HeaderStyle-BackColor="#81DAF5" HorizontalAlign="Left" EnableViewState="false" ForeColor="#333333" GridLines="None" AutoGenerateColumns="true">
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />
<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="true" ForeColor="Navy" />
<Columns>
<asp:BoundColumn DataField="Score" HeaderText="Score TEST" DataFormatString="{0:C}"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
ascx.cs代码
protected static DataTable Pivot1(DataTable tbl)
{
var tblPivot = new DataTable();
tblPivot.Columns.Add("Score", typeof(String));
for (int col = 0; col < tbl.Columns.Count; col++)
{
var r = tblPivot.NewRow();
r[0] = tbl.Columns[col].ToString();
for (int j = 0; j < tbl.Rows.Count; j++)
r[j] = tbl.Rows[j][col];
tblPivot.Rows.Add(r);
}
return tblPivot;
}
protected void GroupButton_Click(object sender, EventArgs e)
{
using (OdbcConnection dbConnection2 = new OdbcConnection(srlsConnStr))
{
dbConnection2.Open();
OdbcCommand dbCommand5 = new OdbcCommand();
dbCommand5.Connection = dbConnection2;
dbCommand5.CommandText = string.Format(@"SELECT TRUNCATE(AVG(s.srls_c1), 2) AS c1_total,
TRUNCATE(AVG(s.srls_c2), 2) AS c2_total,
TRUNCATE(AVG(s.srls_c3), 2) AS c3_total,
TRUNCATE(AVG(s.srls_c4), 2) AS c4_total,
TRUNCATE(AVG(s.srls_c5), 2) AS c5_total,
TRUNCATE(AVG(s.srls_c6), 2) AS c6_total,
TRUNCATE(AVG(s.srls_c7), 2) AS c7_total,
TRUNCATE(AVG(s.srls_c8), 2) AS c8_total
FROM srlsvouchergroup g
JOIN srlsvouchers v ON v.voucher_group_voucher_group_id = g.group_id_pk
AND v.voucher_status = 'Taken'
JOIN srlssurvey s ON s.voucher_no = v.Voucher_no_pk
WHERE g.group_name = ?;");
dbCommand5.Parameters.AddWithValue("@group_name", inst_group_name);
OdbcDataReader dataReader5 = dbCommand5.ExecuteReader();
DataTable dt7 = new DataTable();
dt7.Load(dataReader5);
DataTable dt8 = Pivot1(dt7);
MyDataGrid_2.DataSource = dt8;
MyDataGrid_2.DataBind();
dataReader5.Close();
}
}
发布于 2017-02-21 17:30:50
您似乎使用了错误的DataFormatString
值..。下面这些应该能让你走。
DataFormatString="{0:0.00}"
您可以看到更多的这里。
更新
看起来这可能与您的.ToString();
有关,它没有实现IFormattable
。
使用下面所示的string.Format(...)
,这将为您提供所需的结果:
static void Main(string[] args)
{
double test = 3.00;
Console.WriteLine("number: " + test);
Console.WriteLine("new number: " + string.Format("{0:0.00}", test));
Console.ReadLine();
}
输出:
number: 3
new number: 3.00
因此,您的问题似乎与这一行有关,它在将0发送到网格之前自动截断0:
r[0] = tbl.Columns[col].ToString();
https://stackoverflow.com/questions/42373917
复制相似问题