我创建它使用一个DataGridView创建和修改将在以后导入到系统的本地列表中的应用。它应该由用户编辑(点击并在DGV中编写),并且还应该支持从csv导入,这意味着我需要DGV和数据源之间的双向同步。的DataGridView缺少新的行空数据源
我已经设置DGV的DataSource到BindingList<Client>
这样的:
//In my seperate Client class-file
public class Client
{
private string _name;
private string _mac;
private string _status;
public Client(string pcnavn, string MAC)
{
_pcnavn = pcnavn;
_mac = mac;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string MAC
{
get
{
return _mac;
}
set
{
_mac = value;
}
}
public string Status
{
get
{
return _status;
}
set
{
_status = value;
}
}
}
//In my mainform class
public BindingList<Client> clientDataSource = new BindingList<Client>();
public MainForm()
{
InitializeComponent();
//clienDataGridView.AutoGenerateColumns = false; //The duplicate columns where fixed by setting DataPropertyName for all columns.
clientDataGridView.DataSource = clientDataSource;
}
有了这种方法,DGV产生,但它是空的(只有标题),因此用户不能添加通过使用DGV进行排列。没有DataSource,它会像SQL编辑器一样显示空白行,以便我可以在DGV中手动创建行。如何在链接到空数据源时显示“新项目”-row?我发现的所有例子都使用非空数据源。
AllowUserToAddRows
和AllowUserToDeleteRows
设置为true
。
此图显示了我的问题。使用数据源时,“第一行”丢失。通过输入DGV来添加数据是不可能的。
很高兴您找到解决方案。 – Kurubaran
谢谢,我在设计器中设置了de-bindingsource的AllowNew属性,但仍然无法工作。直到我使用bindingSource的Datasource-property而不是datagridview本身的数据源属性来绑定数据。这工作。 – Peter