C#,SQLite - 计算每个单元格值并放入DataGridView的另一个单元格

时间:2017-09-27 06:12:40

标签: c# datagridview

我想应用公式(Basic_Salary/MonthDays)*Workingdays并将其结果放在Final Amount

this is my datagridview1 in which data fetch from sqlite database 这是我的代码,但是当我运行程序时它会显示:

  

System.InvalidCastException:无法将类型为“System.Windows.Forms.DataGridViewRow”的对象强制转换为“System.Windows.Forms.DataGridView”。

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridView row in dataGridView1.Rows)
    {
        row.SelectedCells[dataGridView1.Columns["Final_Amount"].Index].Value = 
            Convert.ToDouble(row.SelectedCells[dataGridView1.Columns["Basic_Salary"].Index].Value) * 
            Convert.ToDouble(row.SelectedCells[dataGridView1.Columns["WorkingDays"].Index].Value);
    }
}

1 个答案:

答案 0 :(得分:0)

  

注意:点击 Calculate 之后的按钮,而不是在CellLeave个事件中执行这些操作。

工作代码可以是这样的:

数据:

+--+--------------+-------------+---------+--------+
|..| Basic_Salary | WorkingDays | monthNo | YearNo |
+--+--------------+-------------+---------+--------+
|..|        12000 |          20 |       1 |   2017 |
|..|        12000 |          20 |       3 |   2017 |
|..|        13000 |          22 |       2 |   2017 |
|..|        13000 |          21 |       4 |   2017 |
+--+--------------+-------------+---------+--------+

代码:

//// use this condition if you don't have that column in your data-source
if (dataGridView.Columns.Contains("Final_Amount") == false)
{
    dataGridView.Columns.Add("Final_Amount", "Final Amount");
}

dataGridView.Refresh();

//// I extract indexes of my needed columns
var colIndexFinalAmount = dataGridView.Columns["Final_Amount"].Index;
//// when you know `DataPropertyName` of the column instead of its name use a way like this
var colIndexBasicSalary = dataGridView.Columns.OfType<DataGridViewColumn>().SingleOrDefault(w=> w.DataPropertyName == "Basic_Salary").Index;
var colIndexWorkingDays = dataGridView.Columns.OfType<DataGridViewColumn>().SingleOrDefault(w => w.DataPropertyName == "WorkingDays").Index;
var colIndexYearNo = dataGridView.Columns.OfType<DataGridViewColumn>().SingleOrDefault(w => w.DataPropertyName == "YearNo").Index;
var colIndexMonthNo = dataGridView.Columns.OfType<DataGridViewColumn>().SingleOrDefault(w => w.DataPropertyName == "MonthNo").Index;

//// using a simple for is better in this situation    
for (var i = 0; i < dataGridView.RowCount - 1; i++)
{
    //// extract values from your cells but in an expected type (mine is double as my final amount will be double)
    var bs = double.Parse(dataGridView.Rows[i].Cells[colIndexBasicSalary].Value.ToString());
    var wd = double.Parse(dataGridView.Rows[i].Cells[colIndexWorkingDays].Value.ToString());
    //// A way of getting days of a month is this:
    var yn = int.Parse(dataGridView.Rows[i].Cells[colIndexYearNo].Value.ToString());
    var mn = int.Parse(dataGridView.Rows[i].Cells[colIndexMonthNo].Value.ToString());
    var md = (double)DateTime.DaysInMonth(yn, mn);

    //// now, just calculate what you want and set it as `Value` like this:
    dataGridView.Rows[i].Cells[colIndexFinalAmount].Value = bs / md * wd;
}

结果将是:

+--+--------------+-------------+---------+--------+------------------+
|..| Basic_Salary | WorkingDays | monthNo | YearNo |   Final Amount   |
+--+--------------+-------------+---------+--------+------------------+
|..|        12000 |          20 |       1 |   2017 | 7741.93548387097 |
|..|        12000 |          20 |       3 |   2017 | 7741.93548387097 |
|..|        13000 |          22 |       2 |   2017 | 10214.2857142857 |
|..|        13000 |          21 |       4 |   2017 | 9100             |
+--+--------------+-------------+---------+--------+------------------+