【问题标题】:How to add a Cell programmatically to a DataGridView?如何以编程方式将单元格添加到 DataGridView?
【发布时间】: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


【解决方案1】:

两种方式:

  • 您首先需要有一个Column,然后您可以将Column 中的任何Cell 替换为您要创建的任何类型的Cell

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;
  • 或者您创建 Column 以拥有您想要的 ColumnType 并且 Column 中的所有 Cells 从一开始就拥有正确的 Type

DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.HeaderText = "yourHeaderText";
DGV_Points.Columns.Insert(yourColumnIndex, comboCol);

请注意,第一种方法只创建尽可能多的DataGridViewComboBoxCells,而第二种方法会立即在每个Row 中创建一个DataGridViewComboBoxCell。由你决定你想要什么..

在给定位置,您可能只想简单地使用Add ColumnInsertingAdd 方法返回新的ColumnIndex

由于我们要添加DataGridViewComboBoxCells,因此这里有一个示例,您可以如何填充新下拉单元格的Items

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”。请注意,这样您必须填写每个CellItems,并且您可以使用不同的值列表来填写每个选项以供选择。

您也可以单独设置Cell's DataSourceaCell.DataSource = items1;,也可以设置为ColumncomboCol.DataSource = items1;

【讨论】:

  • 感谢您的回复。但是,在对您的第一个代码进行了一些更改后,我设法以编程方式插入了一个新列。所以,我在创建的新单元格中插入值时遇到问题。我已经尝试了以下所有代码: 代码 #1: int i = 0; System.Object obj = (System.Object)DGV_Points.Rows[i].DataBoundItem; obj.Equals(51);代码#2:int i = 0; DGV_Points.Rows[i].Cells[1].Value = 51; DGV_Points.Rows[++i].Cells[1].Value = 35; DGV_Points.Rows.Add();
  • 代码 #3: int i = 0; DGV_Points.EditMode = DataGridViewEditMode.EditProgrammatically; DGV_Points.Rows[i].Cells[1].Value = 51; DGV_Points.Rows[++i].Cells[1].Value = 35; DGV_Points.EndEdit();代码#4:int i = 0;字符串 cellValue = DGV_Points.Rows[i].Cells[1].FormattedValue.ToString(); DGV_Points.Rows[i].Cells[1].Value = 51; DGV_Points.Update();你知道怎么做吗?
  • 填充 Cell 及其 Items 的方法不止一种。我在答案中添加了一个。
【解决方案2】:

你可以这样试试:

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(row);

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多