如何以编程方式将单元格添加到DataGridView? [英] How to add a Cell programmatically to a DataGridView?
问题描述
我想向创建的每一列添加一个额外的单元格,其值来自每一行的单元格。我有以下代码,但不起作用:
I want to add an extra cell to each column created where its value is from the cells in each row. I have this code but doesn't work:
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);
}
但这也不起作用。
有人有什么建议吗?
推荐答案
两种方式:
- 首先需要具有
列
,然后才能替换任何Cell $ c
列
中的$ c>通过要创建的Cell
类型的任何类型:
- You need to have a
Column
first, then you can replace anyCell
in theColumn
by whatever type ofCell
you want to create:
DataGridViewTextBoxColumn normalColumn = new DataGridViewTextBoxColumn();
DGV_Points.Columns.Insert(yourColumnIndex, normalColumn);
DGV_Points.Rows.Add(11); // we need at least one row where we can insert a cell
DataGridViewComboBoxCell aCell = new DataGridViewComboBoxCell();
DGV_Points.Rows[someRowIndex].Cells[yourColumnIndex] = aCell;
- 或者您创建该
列
拥有所需的ColumnType
,并在Column中具有所有
从一开始将具有正确的Cells
Type
: - Or you create that
Column
to have theColumnType
you want and allCells
in theColumn
will have the rightType
from the beginning:
DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.HeaderText = "yourHeaderText";
DGV_Points.Columns.Insert(yourColumnIndex, comboCol);
请注意,第一种方法只会创建尽可能多的 DataGridViewComboBoxCells
如您所愿,而第二种方法立即在每个 Row
中创建一个 DataGridViewComboBoxCell
。
Note that the first method creates only as many DataGridViewComboBoxCells
as you make it to whereas the second way immediatly create a DataGridViewComboBoxCell
in each Row
. It is up to you to decide what you want..
代替或插入
在您可能想要的给定位置只需添加
列
。 Add
方法返回新列
的 Index
。
Instead or Inserting
at a given position you may want to simply Add
the Column
. The Add
method returns the Index
of the new Column
.
由于我们添加了 DataGridViewComboBoxCells
,下面是一个示例,您可以填充项目
的新下拉单元格:
Since we are adding a DataGridViewComboBoxCells
here is an example how you can fill the Items
of the new dropdown cell:
List<string> items1 = new List<string> (){ "111", "222", "333", "444", "555" };
((DataGridViewComboBoxCell)DGV_Points.Rows[2].Cells[0] ).Items
.AddRange(items1.ToArray());
DGV_Points.Rows[2].Cells[0].Value = "222";
这将用五个字符串值填充第2行第0列中单元格的下拉列表,然后选择值为 222。请注意,这种方式必须填充每个 Cell
的 Items
,并且可以用不同的值列表填充每个
This fills the dropdownlist of the cell in row 2, column 0 with five string values and select the value to be "222". Note that this way you must fill the Items
of each Cell
and you can fill each one with a different list of values to choose from.
您还可以设置单个 Cell的数据源
: aCell.DataSource = items1;
或可以在列
中进行设置: comboCol.DataSource = items1;
You can also set an individual Cell's DataSource
: aCell.DataSource = items1;
or you can set it for the Column
: comboCol.DataSource = items1;
这篇关于如何以编程方式将单元格添加到DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!