Datagridview基于另一个单元格值更新单元格值

时间:2019-10-04 17:23:58

标签: c# datagridview calculated-columns

如果我的DataGrid有4列,如你所见

Col1  Col2  Col3  Col4    
5000   -     -    3250    
3250   -     -    2300    
2300   -     -    result

Col1有一个值,Col2和Col3在该值上建立一个公式,Col4打印结果。 我想将该结果设置为Col1的下一个单元格,然后再次使用Col2,3应用公式,依此类推。

我已经尝试了下一个代码段,但是它应用于列中的所有单元格。

for (int i = 1; i < dgv.Rows.Count; i++)
{
   dgv.Rows[i].Cells[1].Value = result;
}

1 个答案:

答案 0 :(得分:0)

了解您要查找的内容有点困难。似乎您想将Column3的结果从上一行输入到下一行。可以这样做:

C#:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>


<div class="card team-card">
  <img src="https://png.pngtree.com/png-vector/20190704/ourlarge/pngtree-vector-user-young-boy-avatar-icon-png-image_1538408.jpg" class="card-img-top" alt="">
  <div class="card-body">
    <h5 class="card-title">John Doe</h5>
    <p class="card-text">Business Director</p>
    <p>Board member (core-team) previous Director-Business Development at EM6 and earlier at Syncada.</p>
  </div>
</div>

VB.NET:

module.exports = {

  attributes: {

  employeeId:{
      type: 'string'
    },
 addressproof:{
      type:______________
},

除了循环整个for (var ir = 1; ir <= dgv.Rows.Count - 1; ir++) // note we're starting on the *2nd* row { dgv.Rows(ir).Cells("Col1").Value = dgv.Rows(ir - 1).Cells("Col3").Value;// or another result from your previous row... } ,您还可以将单个 For ir = 1 To dgv.Rows.Count - 1 ' note we're starting on the *2nd* row dgv.Rows(ir).Cells("Col1").Value = dgv.Rows(ir - 1).Cells("Col3").Value ' or another result from your previous row... Next (单元)的计算添加到适当的事件中。但是除非您有很多行,否则您不会注意到性能差异。

您还可以使用定义的行或单元格简化语法,如下所示:

DataGridview

并应用它:

DataGridViewCell

此外,您应该对设置为DataGridView的DataGridViewRow dr; dr = dgv.Rows(ir); 的{​​{1}}上的值进行操作,而不是对dr.Cells("Col1").value = result; 本身进行操作。为了避免由于性能(当您有很多行时)等原因而遇到错误的数据类型。