【问题标题】:Set DataGridView cell value and add a new row设置 DataGridView 单元格值并添加新行
【发布时间】:2011-05-25 17:41:17
【问题描述】:

我有一个包含两列的 DataGridView。单击第一列中的单元格时,会显示一个 OpenFileDialog,当我选择一个文件时,第二列中的单元格值将设置为选定的文件名。代码如下:

private void dataGridView1_CellContentClick(
    object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        SelectFile(e.RowIndex);
    }
}

private void SelectFile(int rowIndex)
{
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        dataGridView1.Rows[rowIndex].Cells[1].Value =
            openFileDialog.FileName;
    }
}

这可行,但我希望在设置单元格值时添加一个新行。当我手动编辑单元格时会发生这种情况,该行进入“编辑”状态并在下面添加一个新行。当我以编程方式设置单元格值时,我希望同样的事情发生。

[编辑]

当我的表单第一次显示时,DataGridView 是空的,唯一显示的行是带有 (* 符号 - IsNewRow 为真) 的行。当我手动编辑该行中的一个单元格时,它会进入编辑状态(铅笔符号)并添加一个新的空行。当我使用上面的代码时,这不会发生。该行保持为 IsNewRow(* 号),不添加新行。

【问题讨论】:

  • 我试图用 Add 方法做到这一点,但行被添加到第一个位置。我能想到的唯一解决方案是添加新行,然后将值与当前行交换。 Probaj, možda uspe :)
  • 是的,我可能必须这样做,但即使我用代码编辑第一行,添加新行似乎也是合乎逻辑的。 Hvala na komentaru。
  • 是的,显然这是DataGridView的一个“特性”(不是说bug)。也许这是有原因的。尝试在msdn上询问。斯雷奇诺!

标签: c# winforms datagridview


【解决方案1】:

使用此代码,您可以做您想做的事。我出于同样的原因使用它:

myGrid.NotifyCurrentCellDirty(true);

此答案的来源: Make new row dirty programmatically and insert a new row after it in DataGridView

【讨论】:

  • 该链接现在有一个答案,在myGrid.NotifyCurrentCellDirty(true); 之后添加myGrid.NotifyCurrentCellDirty(false); 对我来说,这有助于删除 myGrid.NotifyCurrentCellDirty(true); 的“正在编辑的行”标记;原因。
【解决方案2】:

我遇到了同样的问题。没有找到更优雅的解决方案,但认为提供一些代码可能会有所帮助:

//...
    DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.OK)
        {
            DataGridViewRow curRow = cell.DataGridView.Rows[cell.RowIndex];
            // check if the current row is the new row
            if (curRow.IsNewRow)
            {
                // if yes, add a new one
                int newRowIdx = cell.DataGridView.Rows.Add();
                DataGridViewRow newRow = cell.DataGridView.Rows[newRowIdx];
                DataGridViewCell newCell = newRow.Cells[cell.ColumnIndex];
                // check if the new one is _not_ the new row (this is the unexpected behavior mentioned in the questions comments)
                if (!newRow.IsNewRow)
                    newCell.Value = ofd.FileName;
                else if (!curRow.IsNewRow)
                    cell.Value = ofd.FileName;
            }
            else {
                cell.Value = ofd.FileName;
            }
        }

【讨论】:

    【解决方案3】:

    我也遇到了同样的问题。我做了类似以下的事情,它只是工作! (.Net 框架 4.5)

    // Set value for some cell, assuming rowIndex refer to the new row.
    dateGridView1.Rows[rowIndex].Cells[1].Value = "Some Value";
    
    // Then simply do this:
    dataGridView1.BeginEdit(true);
    dataGridView1.EndEdit();
    

    【讨论】:

    • 干得好!我遇到了一个问题,将 excel 文件适应 datagridview 并使用“dateGridView1.Rows[rowIndex].Cells[1].Value = “Some Value”;”我解决了!现在我可以上传 xls 或 xlsx 文件,而与行和列大小无关,我的 datagridview 已成功填充!
    【解决方案4】:

    如果它正在加载,您需要先添加新行,然后为它的单元格赋予值,请执行以下操作。

    当我试图用某些 Datatable 的某些列填充 Datagrid 的某些列并从 SQL 数据库中检索其数据时,我使用了这个:

           if (dtbl.Rows.Count > 0)
            {
                for (int i = 0; i < dtbl.Rows.Count; i++)
                {
                        // adding a row each time it loops in and find a row in the dtbl
                        dataGridView1.Rows.Add();
    
                        dataGridView1.Rows[i].Cells[0].Value = dtbl.Rows[i][0].ToString();
                        dataGridView1.Rows[i].Cells[1].Value = dtbl.Rows[i][1].ToString();
                        dataGridView1.Rows[i].Cells[2].Value = dtbl.Rows[i][2].ToString();
                }
                dataGridView1.ReadOnly = true;
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }  
    

    【讨论】:

      【解决方案5】:

      我遇到了这个问题并以不同的方式解决了它。

      基本上,用户单击DataGridView 上的一个按钮,它会打开一个询问问题的对话框,然后填写该行。只是为了好玩,我通过使用SendKeys 将行修复为编辑模式而不是新模式。您不必使用SendKeys 填写整行,只需一个单元格即可。

      需要注意两件重要的事情。首先,datagridview 需要关闭多选,或者您需要取消选择当前单元格。其次,您需要 defaultvaluesneeded 事件来工作并用健全的数据填充网格。

      myRow.Cells("dgvStockNumber").Selected = True
      OrderDetailDataGridView.BeginEdit(True)
      SendKeys.Send("{Backspace}")
      SendKeys.Send(scp.MyStockCard.StockNumber)
      Application.DoEvents()
      OrderDetailDataGridView.EndEdit()
      myRow.Cells("dgvChooseStockCard").Selected = True
      OrderDetailDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit)
      

      有更好的方法来解决这个问题,但这很容易哈哈。

      【讨论】:

        【解决方案6】:

        不清楚您要做什么,但您可以像这样向 DataGridView 添加新行:

        dataGridView1.Rows.Add()
        

        .Add() 方法被重载,因此请检查哪个 Add 方法是您在 Visual Studio 中想要的方法。

        【讨论】:

        • 感谢您的回答。我编辑了我的问题,希望现在很清楚。
        【解决方案7】:

        我搜索了如何插入新行以及如何像 Excel 一样设置其中的单元格的单个值的解决方案。我用以下代码解决了:

        dataGridView1.ReadOnly = false; //Before you modify it, it should be set to false!
        
        dataGridView1.Rows.Add(); //This inserts first row, index is "0"
        dataGridView1.Rows[0].Cells[0].Value = "this is 0,0!"; //This line will set the right hand string to the very first cell of your datagridview.
        

        注意: 1.之前我在设计模式下设计了列。 2. 我已从 datagridview 的属性中将行标题可见性设置为 false。

        希望这对你有帮助。

        【讨论】:

          猜你喜欢
          • 2019-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多