【问题标题】:How to set Cell value of DataGridViewRow by column name?如何通过列名设置 DataGridViewRow 的单元格值?
【发布时间】:2014-04-03 02:53:05
【问题描述】:

在 Windows 窗体中,我试图通过插入 DataGridViewRows 手动填充 DataGridView,所以我的代码如下所示:

DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvArticles);
row.Cells[0].Value = product.Id;
row.Cells[1].Value = product.Description;
.
.
.
dgvArticles.Rows.Add(row);

但是,我想按列名而不是按索引添加单元格值,如下所示:

row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;

但是这样做会引发错误,提示找不到名为“code”的列。 我正在从设计器中设置 DataGridView 列,如下所示:

我做错了吗?我怎样才能完成我想做的事情?

【问题讨论】:

    标签: c# winforms datagridview datagridviewrow


    【解决方案1】:

    因此,为了实现您希望的方法,需要以这种方式完成:

    //Create the new row first and get the index of the new row
    int rowIndex = this.dataGridView1.Rows.Add();
    
    //Obtain a reference to the newly created DataGridViewRow 
    var row = this.dataGridView1.Rows[rowIndex];
    
    //Now this won't fail since the row and columns exist 
    row.Cells["code"].Value = product.Id;
    row.Cells["description"].Value = product.Description;
    

    【讨论】:

    • row.Cells["code"].Value 应修改为 row.Cells["Code"].Value,首字母大写。或 row.Cells[code.Name].Value,首字母小写。
    • @BravoYeung:根据 OP 提供的屏幕截图:Name 属性设置为“code”(小写),而HeaderText 属性设置为“Code”(大写)。在这种情况下 - 您需要通过索引器引用列 Name 而不是 HeaderText
    【解决方案2】:

    我也试过了,结果是一样的。这有点冗长,但它有效:

    row.Cells[dataGridView1.Columns["code"].Index].Value = product.Id;
    

    【讨论】:

    • row.Cells["code"].Value 应修改为 row.Cells["Code"].Value,首字母大写。或 row.Cells[code.Name].Value,首字母小写。
    【解决方案3】:

    当您使用 DataGridViewCellCollection 的 ColumnName 索引器时,它会在内部尝试使用来自此 DataGridViewRow 实例的拥有/父 DataGridView 的 ColumnName 获取列索引。在您的情况下,该行尚未添加到 DataGridView 中,因此拥有的 DataGridView 为空。这就是为什么您会收到 找不到名为 code 的列的错误。

    IMO 最好的方法(与 Derek 相同)是在 DataGridView 中添加行并使用返回的索引从网格中获取行实例,然后使用列名访问单元格。

    【讨论】:

      【解决方案4】:

      问题在于,在将行添加到 DataGridView 之前,按名称引用单元格不起作用。在内部,它使用 DataGridViewRow.DataGridView 属性来获取列名,但在添加行之前该属性为空。

      利用C#7.0的本地函数特性,代码可以半读。

      DataGridViewRow row = new DataGridViewRow();
      row.CreateCells(dgvArticles);
      
      DataGridViewCell CellByName(string columnName)
      {
          var column = dgvArticles.Columns[columnName];
          if (column == null)
              throw new InvalidOperationException("Unknown column name: " + columnName);
          return row.Cells[column.Index];
      }
      
      
      CellByName("code").Value = product.Id;
      CellByName("description").Value = product.Description;
      .
      .
      .
      dgvArticles.Rows.Add(row);
      

      【讨论】:

        【解决方案5】:

        另一种选择:
        假设您的 DataGridView 的名称是 dataGridView1

        var row = new DataGridViewRow();
        // Initialize Cells for this row
        row.CreateCells(_dataGridViewLotSelection);
        
        // Set values
        row.Cells[dataGridView1.Columns.IndexOf(code)].Value = product.Id;
        row.Cells[dataGridView1.Columns.IndexOf(description)].Value = product.Description;
        // Add this row to DataGridView
        dataGridView1.Rows.Add(row);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-06
          • 1970-01-01
          • 1970-01-01
          • 2011-10-30
          • 2011-04-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多