简体   繁体   English

在DataGridView中完成添加行

[英]Complete Adding Row in DataGridView

I'm sure that there's a simple answer to this but for the life of me I can't see it: 我敢肯定对此有一个简单的答案,但对于我的一生,我看不到:

I have a DataGridView bound to a DataTable. 我有一个绑定到DataTable的DataGridView。 One of the cells can contain fairly lengthy strings so I've created a editor dialog window (which also does some validation etc.) which opens on the DataGridView's CellDoubleClick. 其中一个单元格可以包含相当长的字符串,因此我创建了一个编辑器对话框窗口(它也进行了一些验证等),该窗口在DataGridView的CellDoubleClick上打开。 When the user clicks ok on the editor dialog the value gets put in the cell. 当用户在编辑器对话框上单击“确定”时,该值将被放入单元格中。

This works fine as long as it's a cell that has already being added to the DataGridView, but if I do it on the "add new row" (where DataGridViewRow.IsNewRow is true) then when the dialog is closed I have the double problem of the edited string not being displayed unless the row is in edit mode AND subsequently not being able to add a new row. 只要它是已经添加到DataGridView的单元格,它就可以正常工作,但是如果我在“添加新行”(其中DataGridViewRow.IsNewRow为true)上进行操作,那么在关闭对话框时,我会遇到双重问题除非该行处于编辑模式并且随后无法添加新行,否则不会显示已编辑的字符串。

One potential solution I can see is ensuring that the new row is added to the underlying source before I open the dialog window but I can't see an elegant way to do that (DataGridView.EndEdit() doesn't do it). 我可以看到的一个潜在解决方案是,确保在打开对话框窗口之前将新行添加到基础源中,但是我看不到一种完美的方法(DataGridView.EndEdit()不能这样做)。 So is there a neat way to do that (or alternatively is there a better solution altogether)? 那么,有没有一种整洁的方法可以做到这一点(或者完全有更好的解决方案)?

Code goes something like this: 代码如下所示:

void DataGridViewCellCoubleClick(object sender, DataGridViewCellEventArgs e){
    DataGridView gridview = (DataGridView)sender;
    gridview.EndEdit();
    EditForm editForm = new EditForm(gridview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

    if(editForm.ShowDialog() == DialogResult.OK){
        gridview.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = editForm.StringResult;
    }

}

EDIT: 编辑:

I found a solution which achieves what I require: 我找到了可以满足我要求的解决方案:

Before calling : 致电之前:

DataGridView.EndEdit()

I also check for the current row to be "IsNewRow" and if it is I use 我还检查当前行是否为“ IsNewRow”,如果使用

DataGridView.NotifyCurrentCellDirty(true)

Which adds it before I open my edit window. 在我打开编辑窗口之前添加了它。 Hope that helps someone. 希望能对某人有所帮助。

I think the problem is with the way that you have bound the Data Table to the Data GridView. 我认为问题在于将数据表绑定到数据GridView的方式。

I would create a binding source and set its Datasource to the DataTable. 我将创建一个绑定源并将其Datasource设置为DataTable。 I would then Set The Data Source for your Datagrid View as the Binding Source. 然后,我会将您的Datagrid视图的数据源设置为绑定源。

When you add a new row in the Datagrid View, create a new row in your datatable, then refresh the Datatable. 在Datagrid视图中添加新行时,请在数据表中创建新行,然后刷新数据表。

Those changes should reflect through in the Datagrid view as they are both bound by the Binding source. 这些更改都应在Datagrid视图中反映出来,因为它们都受Binding源绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM