简体   繁体   English

从 BindingSource 中删除项目会在作为 DataSource 连接的 DataGridView 中留下空白行

[英]Deleting Item from BindingSource Leaves Blank Row in DataGridView Which it is Connected to as DataSource

I am working on a winforms project where I have a bindingsource connected to a DataGridView.我正在开发一个 winforms 项目,其中我有一个连接到 DataGridView 的绑定源。 Adding and updating work like a charm, but when I remove an item, it leaves a blank row.添加和更新工作就像一个魅力,但是当我删除一个项目时,它会留下一个空白行。 Any item added after this, is added after the blank row.在此之后添加的任何项目都将添加到空白行之后。 I tried refreshing the DataGridView, set AllowUserToAddRows to False, and a few other suggestions I found on StackOverflow.我尝试刷新 DataGridView,将 AllowUserToAddRows 设置为 False,并在 StackOverflow 上找到了一些其他建议。 Any help would be appreciated to solve this issue.任何帮助将不胜感激来解决这个问题。 Thanks.谢谢。

Here is the code I have so far.这是我到目前为止的代码。

// Setting the binding source
IList<Contact> contacts = getContacts();
contactBindingSource.DataSource = contacts;

// removing the item
var currentContact = (Contact)dataGridView1.CurrentRow.DataBoundItem;
contactBindingSource.Remove(currentContact);

you can first remove item from gridview then remove from data source您可以先从 gridview 中删除项目,然后从数据源中删除

 int x = dataGridView1.CurrentRow.Index;
 dataGridView1.Rows.Remove(DataGridView1.Rows[x]); 
 contactBindingSource.DataSource = dataGridView1.DataSource as DataTable;

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

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