C# 网格将值从一列复制到另一列

C# 网格将值从一列复制到另一列,c#,C#,我在网格中有以下格式的数据 Code Option1 Option2 ---------------------------------------- Finance Charges 100 0 Insurance Charges 200 0 Other Charges 300 0 Bank Charges 400 0 我想使用代码将值从选项1列复制到选项2列 Co

我在网格中有以下格式的数据

Code                Option1    Option2
----------------------------------------
Finance Charges     100        0
Insurance Charges   200        0
Other Charges       300        0
Bank Charges        400        0
我想使用代码将值从选项1列复制到选项2列

Code                Option1    Option2
----------------------------------------
Finance Charges     100        100
Insurance Charges   200        200
Other Charges       300        300
Bank Charges        400        400
等待您的答复

谢谢。

for(int rows=0;rowsfor (int rows = 0; rows < dataGrid.Rows.Count; rows++).ToString()
{
        dataGrid.Rows[rows].Cells[2].Value=dataGrid.Rows[rows].Cells[1].Value;
} 
{ dataGrid.Rows[Rows]。单元格[2]。值=dataGrid.Rows[Rows]。单元格[1]。值; }
既然您没有解释您的案例是针对Winforms还是WPF,我假设它是Winforms。下面基于linq的单行程序将有助于您的案例

dataGridView.Rows.Cast<DataGridViewRow>().ToList().ForEach(x => x.Cells[2].Value = x.Cells[1].Value);
dataGridView.Rows.Cast().ToList().ForEach(x=>x.Cells[2].Value=x.Cells[1].Value);

我们用于迭代所有项的ForEach linq方法仅在实现IList的类型上可用,另一方面,Rows是一个DataGridViewRowCollection,仅实现IEnumerable,因此我们在应用转换之前将其转换为List。

为每一行将列选项1的值分配给选项2

for (int i = 0; i < dataGrid.Rows.Count; i++)
{
        dataGrid.Rows[i].Cells["Option2"].Value=dataGrid.Rows[i].Cells["Option1"].Value;
} 
for(int i=0;i
什么小部件框架?到目前为止你试过什么?您遇到了什么问题?您使用了哪种类型的应用程序
WinForm
WebForm
,还是其他类型的应用程序?