C# 在DataGridView中测试最后一项空白对象
早上好, 我正在使用数据集将xml导入DataGridView,对DataGridView中的空白对象值进行一些奇怪的行为测试。我刚刚开始研究在WinForm中使用DataGridView。我使用AuthorsDataSet演练创建了非常简单的DataGridView 然后,我对代码进行了非常小的操作,以读取我的xml文件,而不是AuthorsDataSet。效果很好 然后,我尝试将所有项目汇总到一列中。我找到了这个,它让我明白了我的意思 这让我想到这里C# 在DataGridView中测试最后一项空白对象,c#,xml,winforms,datagridview,C#,Xml,Winforms,Datagridview,早上好, 我正在使用数据集将xml导入DataGridView,对DataGridView中的空白对象值进行一些奇怪的行为测试。我刚刚开始研究在WinForm中使用DataGridView。我使用AuthorsDataSet演练创建了非常简单的DataGridView 然后,我对代码进行了非常小的操作,以读取我的xml文件,而不是AuthorsDataSet。效果很好 然后,我尝试将所有项目汇总到一列中。我找到了这个,它让我明白了我的意思 这让我想到这里 public string
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();
}
请提前告知并感谢您。我很感激 试试这个
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();
}
公共字符串和()
{
双和=0;
串试验;
对于(int i=0;i
string test=dataGridView1.Rows[i].Cells[6].Value.ToString()
在有问题的xml处为我提供了一个NullReference异常。不过谢谢你:-)看起来很不错。我忘记了声明字符串然后分配它。我不以写代码为生,所以可能需要几个月的时间。非常感谢。你得到了奖品,但我肯定会给你更多的反馈。谢谢!!:-我能不能说服你不要对我的一条评论投赞成票,这样我就可以做其他网站的事情??:-D请你不要这么做。我需要一票才能做其他事情。那我就等20或50分钟。我不记得了。一切都很好。不过,我会让你了解最新情况。再次感谢您的帮助。我真的很感谢你的帮助-I’祝你今天愉快!!
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();
}