【发布时间】:2015-04-27 11:03:49
【问题描述】:
我想向创建的每一列添加一个额外的单元格,其值来自每一行中的单元格。我有这段代码,但不起作用:
i = 0;
Double X1 = Convert.ToDouble(DGV_Points.Rows[i].Cells[6].Value);
Double X2 = Convert.ToDouble(DGV_Points.Rows[i++].Cells[6].Value);
Double LapLength = X1 - X1;
this.DGV_Points.Rows.Add();
this.DGV_Points.Rows[0].Cells[1].Value = LapLength;
this.DGV_Points.Rows[1].Cells[1].Value = LapLength;
我也试过这个例子:
foreach (var item in _model.SelectedItems)
{
var row = new DataGridViewRow();
var codeCell = new DataGridViewComboBoxCell();
codeCell.Items.AddRange(ItemCode.Items);
codeCell.Value = ItemCode.Items.GetListItem(item.Code);
var nameCell = new DataGridViewComboBoxCell();
nameCell.Items.AddRange(ItemName.Items);
nameCell.Value = ItemName.Items.GetListItem(item.Name);
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Id });
row.Cells.Add(codeCell);
row.Cells.Add(nameCell);
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Units });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Quantity });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceLt });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceEu });
itemView.Rows.Add(row);
}
但它也不起作用。
有人有什么建议吗?
【问题讨论】:
-
顺便说一句,“.Rows[i++]”并没有像你想象的那样做。它仍然返回第 0 行。您想改用 ++i。
标签: c# datagridview cell