如何以编程方式将单元格添加到DataGridView? [英] How to add a Cell programmatically to a DataGridView?

查看:76
本文介绍了如何以编程方式将单元格添加到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>通过要创建的 Cell 类型的任何类型:

  • You need to have a Column first, then you can replace any Cell in the Column by whatever type of Cell 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 the ColumnType you want and all Cells in the Column will have the right Type 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆