测试 DataGridView 中的最后一个条目空白对象
Testing for last entry blank objects in a DataGridView
早上好;
我正在对 DataGridView 中的空白对象值进行一些奇怪的行为测试,使用 DataSet 将 xml 导入 DataGridView。我刚刚开始研究在我的 WinForm 中使用 DataGridView。我使用 AuthorsDataSet 演练创建了非常简单的 DataGridView
https://msdn.microsoft.com/en-us/library/ekw4dh3f.aspx
然后我对代码进行了非常轻微的操作,以读取我的 xml 文件而不是 AuthorsDataSet。效果很好。
然后我尝试对一栏中的所有项目求和。我找到了这个,它让我到了现在的地步。
how I can show the sum of in a datagridview column?
这让我来到这里
public string sum()
{
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
var test = dataGridView1.Rows[i].Cells[6].Value; //Can not use
//double here as I get the FromatException whether
//testing for `==null` or `==""`
//double test = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value); THIS THROWS FormatException
if (test == "")
{
sum += 0;
}
else
{
sum += Convert.ToDouble(test);
}
}
return sum.ToString();
}
如果我使用 double test...
和 if (test == null)
我会得到 FormatException was unhandled
最终我所拥有的有效,但我担心将来会出现其他错误。问题在于,我正在测试的 xml 对象可能没有任何价值,而且我不确定该把它放在哪里。我是否应该为它分配一个值 0,然后在用户提供输入时覆盖它(在这种情况下,它是一个简单的 start/stop 计时器。)
如果我只是使用 sum += Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value);
而没有测试空白 xml 对象,我会收到 FormatException 错误。
我怎么测试错了。示例 xml 条目如下:
<?xml version="1.0" encoding="utf-8"?>
<Form1>
<Name Key="4/13/2015 3:22:05 PM">
<Date>4/13/2015</Date>
<JobNum>01det</JobNum>
<RevNum>00000</RevNum>
<Task>testing</Task>
<Start>12:30 PM</Start>
<End>03:22 PM</End>
<TotalTime>9828063</TotalTime>
</Name>
<Name Key="4/14/2015 6:36:06 AM">
<Date>4/14/2015</Date>
<JobNum>01det</JobNum>
<RevNum>00000</RevNum>
<Task>testing</Task>
<Start>06:36 AM</Start>
<End></End> \THIS ONE WILL ALMOST ALWAYS BE BLANK DURING REGULAR BUSINESS HOURS
<TotalTime></TotalTime>
</Name>
</Form1>
请提前告知并谢谢您。我很感激!!
试试这个。
public string sum()
{
double sum = 0;
string test;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
if (dataGridView1.Rows[i].Cells[6].Value != null)
{
test = dataGridView1.Rows[i].Cells[6].Value.ToString();
if (test != "")
{
sum += double.Parse(test);
}
}
}
return sum.ToString();
}
早上好;
我正在对 DataGridView 中的空白对象值进行一些奇怪的行为测试,使用 DataSet 将 xml 导入 DataGridView。我刚刚开始研究在我的 WinForm 中使用 DataGridView。我使用 AuthorsDataSet 演练创建了非常简单的 DataGridView
https://msdn.microsoft.com/en-us/library/ekw4dh3f.aspx
然后我对代码进行了非常轻微的操作,以读取我的 xml 文件而不是 AuthorsDataSet。效果很好。
然后我尝试对一栏中的所有项目求和。我找到了这个,它让我到了现在的地步。
how I can show the sum of in a datagridview column?
这让我来到这里
public string sum()
{
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
var test = dataGridView1.Rows[i].Cells[6].Value; //Can not use
//double here as I get the FromatException whether
//testing for `==null` or `==""`
//double test = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value); THIS THROWS FormatException
if (test == "")
{
sum += 0;
}
else
{
sum += Convert.ToDouble(test);
}
}
return sum.ToString();
}
如果我使用 double test...
和 if (test == null)
我会得到 FormatException was unhandled
最终我所拥有的有效,但我担心将来会出现其他错误。问题在于,我正在测试的 xml 对象可能没有任何价值,而且我不确定该把它放在哪里。我是否应该为它分配一个值 0,然后在用户提供输入时覆盖它(在这种情况下,它是一个简单的 start/stop 计时器。)
如果我只是使用 sum += Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value);
而没有测试空白 xml 对象,我会收到 FormatException 错误。
我怎么测试错了。示例 xml 条目如下:
<?xml version="1.0" encoding="utf-8"?>
<Form1>
<Name Key="4/13/2015 3:22:05 PM">
<Date>4/13/2015</Date>
<JobNum>01det</JobNum>
<RevNum>00000</RevNum>
<Task>testing</Task>
<Start>12:30 PM</Start>
<End>03:22 PM</End>
<TotalTime>9828063</TotalTime>
</Name>
<Name Key="4/14/2015 6:36:06 AM">
<Date>4/14/2015</Date>
<JobNum>01det</JobNum>
<RevNum>00000</RevNum>
<Task>testing</Task>
<Start>06:36 AM</Start>
<End></End> \THIS ONE WILL ALMOST ALWAYS BE BLANK DURING REGULAR BUSINESS HOURS
<TotalTime></TotalTime>
</Name>
</Form1>
请提前告知并谢谢您。我很感激!!
试试这个。
public string sum()
{
double sum = 0;
string test;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
if (dataGridView1.Rows[i].Cells[6].Value != null)
{
test = dataGridView1.Rows[i].Cells[6].Value.ToString();
if (test != "")
{
sum += double.Parse(test);
}
}
}
return sum.ToString();
}