从DataGridView中的用户输入数据中获取总和 - C#

时间:2016-05-13 13:16:17

标签: c# winforms datagridview

我试图获取用户在DataGridView单元格中输入的数据总和。

这是我的代码:

  private void dataGridView1_CellValidated(object sender,      DataGridViewCellEventArgs e)
         {
             if (e.RowIndex > -1)
             {
                 DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

                 string monday = row.Cells[Column4.Index].Value.ToString();
                 string tuesday = row.Cells[Column5.Index].Value.ToString();
                 string wednesday = row.Cells[Column6.Index].Value.ToString();
                 string thursday = row.Cells[Column7.Index].Value.ToString();
                 string friday = row.Cells[Column8.Index].Value.ToString();
                 string saturday = row.Cells[Column9.Index].Value.ToString();
                 string sunday = row.Cells[Column10.Index].Value.ToString();

                 int result;

                 if (Int32.TryParse(monday, out result) && Int32.TryParse(tuesday, out result) && Int32.TryParse(wednesday, out result) && Int32.TryParse(thursday, out result) && Int32.TryParse(friday, out result) && Int32.TryParse(saturday, out result) && Int32.TryParse(sunday, out result))
                 {
                     row.Cells[Column11.Index].Value = monday + tuesday + wednesday + thursday + friday + saturday + sunday;
                 }
             }
         }

每次运行此代码时,都会遇到NullReferenceException。

有什么想法吗?

0 个答案:

没有答案