2017-09-27 51 views
0

的另一个细胞:C#,SQLite的 - 计算每个细胞值,放在我想申请公式<code>(Basic_Salary/MonthDays)*Workingdays</code>,放在<code>Final Amount</code>其结果的DataGridView

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); 
    } 
} 
+0

相反使用'foreach'使用简单的'for',然后像'dataGrid.Rows [row] .Cells [colIndex] .Value' -HTH;)那样访问它。 –

+0

你想要生成所有的最终总数?或者只有用户离开的单元格?哼。如果你想产生所有的最终总数,然后尝试修改你的代码。 'foreach(dataGridView1.Rows中的DataGridView行) {row.Cells [“FinalTotal”]。Value = Convert.ToDouble(row.Cells [“Basic_Salary”]。Value)* Convert.ToDouble(row.Cells [“WorkingDays “]。值); }' – Muj

+0

谢谢,但是当我尝试将单元格转换为int或任何显示无法将对象值转换为int – Aravi

回答

0

注意:不能做这些内部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    | 
+--+--------------+-------------+---------+--------+------------------+ 
相关问题