隐藏

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

发布:2022/11/24 16:11:43作者:管理员 来源:本站 浏览次数:494

我有两列的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为true)的行。 当我手动编辑该行中的单元格时,它将进入编辑状态(铅笔符号),并添加了一个新的空行。 当我使用上面的代码时,不会发生这种情况。 该行仍为IsNewRow(*符号),并且不添加新行。


enter image description here

enter image description here

相关讨论


   我试图用Add方法做到这一点,但是将行添加到第一个位置。 我能想到的唯一解决方案是添加新行,而不是与当前行交换值。 Probaj,mo?da uspe :)

   是的,我可能必须这样做,但是即使我使用代码编辑了第一行,添加新行似乎也合乎逻辑。 赫瓦拉纳科门塔鲁。

   是的,显然它是DataGridView的"功能"(更不用说bug)。 也许是有原因的。 尝试询问msdn。 是吗?


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

1


myGrid.NotifyCurrentCellDirty(true);


该答案的来源:

以编程方式使新行变脏,然后在DataGridView中插入新行

相关讨论


   该链接现在有一个答案,该答案在myGrid.NotifyCurrentCellDirty(true);之后添加了myGrid.NotifyCurrentCellDirty(false);。对我来说,这有助于删除正在编辑的行,标记为myGrid.NotifyCurrentCellDirty(true);。 原因。


我也遇到了同样的问题。我做了以下类似的事情,它就可以了!!! (.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成功地成功填充了!


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



//...

   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;

           }

       }


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


当我尝试用某些数据表的某些列填充数据网格的某些列并从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;

       }


我遇到了这个问题,并以另一种方式解决了它。


基本上,用户单击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)


有更好的方法可以解决此问题,但这很简单。


我搜索了如何插入新行以及如何像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。


希望这对您有帮助。


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



dataGridView1.Rows.Add()


.Add()方法已重载,因此请检查哪种Add方法是Visual Studio中所需的方法。