设置DataGridView单元格值并添加新行

问题描述

| 我有两列的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;
    }
}
这可行,但是我希望在设置单元格值时添加新行。当我手动编辑单元格时,会发生这种情况,该行进入\'edit \'状态,并在下面添加新行。我希望以编程方式设置单元格值时发生同样的情况。 [编辑] 第一次显示我的表单时,DataGridView为空,唯一显示的行是带有(*符号-IsNewRow为true)的行。当我手动编辑该行中的单元格时,它将进入编辑状态(铅笔符号),并添加了一个新的空行。当我使用上面的代码时,不会发生这种情况。该行仍为IsNewRow(*符号),并且不添加新行。     

解决方法

我遇到了同样的问题。没想出一个更优雅的解决方案,但认为提供一些代码可能会有所帮助:
//...
    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;
            }
        }
    ,我也遇到了同样的问题。我做了类似以下的事情,它可以正常工作!!! (.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();
    ,使用此代码,您可以做您想做的事情。我出于相同的原因使用它:
myGrid.NotifyCurrentCellDirty(true);
该答案的来源: 以编程方式使新行变脏,然后在DataGridView中插入新行     ,如果正在加载,则需要先添加新行,然后为它的单元格赋予一个值,然后执行以下操作。 当我尝试用某些数据表的某些列填充数据网格的某些列并从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上的按钮,它会打开一个对话框,询问问题,然后将其填满。只是为了好玩,我通过使用
SendKeys
修复了它,使行处于编辑模式而不是新模式。您不必使用ѭ6来填充整个行,只需一个单元格就足以使它工作。 需要注意的两个重要事项。首先,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)
有更好的方法可以解决此问题,但这很简单。     ,目前尚不清楚您要做什么,但是您可以像这样向DataGridView添加新行:
dataGridView1.Rows.Add()
.Add()方法已重载,因此请检查哪种
Add
方法是Visual Studio中所需的方法。     ,我搜索了如何插入新行以及如何像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。 希望这对您有帮助。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...