conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT sum_count, pass_count, fail_count FROM test_result", conn); DataTable dt = new DataTable(); adapter.Fill(dt); conn.Close(); if (dt.Rows.Count > 0) { totalCount = Convert.ToInt32(dt.Rows[0]["sum_count"]); okCount = Convert.ToInt32(dt.Rows[0]["pass_count"]); ngCount = Convert.ToInt32(dt.Rows[0]["fail_count"]); double passRate = (double)okCount / totalCount; txt_passrate.Text = passRate.ToString("P2"); //passRate = Convert.ToInt32(dt.Rows[0]["pass_rate"]); txt_sumcount.Text = " " + totalCount.ToString(); txt_passcount.Text = " " + okCount.ToString(); txt_failcount.Text = " " + ngCount.ToString(); //txt_passrate.Text = " " + passRate.ToString(); },以上代码中,当okcount,ngcount,totalcount数据都清零时,下次打开软件,txt_passrate.Text组件显示NAN,怎么让它显示0%
时间: 2023-06-19 12:05:31 浏览: 145
c#增删查改
可以在计算通过率之前,检查 `totalCount` 是否为零,如果为零,则直接将通过率设置为0,如下所示:
```
if (dt.Rows.Count > 0) {
totalCount = Convert.ToInt32(dt.Rows[0]["sum_count"]);
okCount = Convert.ToInt32(dt.Rows[0]["pass_count"]);
ngCount = Convert.ToInt32(dt.Rows[0]["fail_count"]);
if (totalCount == 0) {
txt_passrate.Text = "0%";
} else {
double passRate = (double)okCount / totalCount;
txt_passrate.Text = passRate.ToString("P2");
}
txt_sumcount.Text = " " + totalCount.ToString();
txt_passcount.Text = " " + okCount.ToString();
txt_failcount.Text = " " + ngCount.ToString();
}
```
这样,当 `totalCount` 为零时,就会直接将 `txt_passrate.Text` 设置为 "0%"。
阅读全文