【问题标题】:Testing for last entry blank objects in a DataGridView测试 DataGridView 中的最后一个条目空白对象
【发布时间】:2015-06-20 01:52:22
【问题描述】:

早安;

我正在使用 DataSet 将 xml 导入 DataGridView 中对 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,然后在用户提供输入时覆盖它(在这种情况下,它是一个简单的启动/停止计时器。)

如果我只使用 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>

请提前告知并感谢您。我很感激!!

【问题讨论】:

    标签: c# xml winforms datagridview


    【解决方案1】:

    试试这个。

     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();
        }
    

    【讨论】:

    • string test = dataGridView1.Rows[i].Cells[6].Value.ToString(); 在有问题的 xml 中为我提供了 NullReference 异常。不过谢谢你:-)
    • 看起来很不错。我忘记了声明字符串然后分配它。我不以编码为生,因此可能需要几个月的时间。谢谢。你得到了奖品,但我肯定会为你提供更多反馈。谢谢!! :-D
    • 我可以说服你不要对我的一个 cmets 投赞成票(碰撞),这样我就可以做其他网站的事情了吗? :-D 请请
    • 不,你不必这样做。我需要一票才能做其他事情。然后我持有20或50。我不记得了。都很好。不过,我会及时通知您。再次感谢您的协助。我真的很感激帮助。 :-D 祝你有美好的一天!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多