2013-12-14 35 views
4

我创建它使用一个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?我发现的所有例子都使用非空数据源。

AllowUserToAddRowsAllowUserToDeleteRows设置为true

此图显示了我的问题。使用数据源时,“第一行”丢失。通过输入DGV来添加数据是不可能的。 DGV

回答

9

MSDN看,我发现这一点:

如果在DataGridView绑定到数据,允许用户添加行如果此属性和数据源的IBindingList.AllowNew属性均设置为true。

看来BindingList具有这个属性为false默认情况下,所以这个小动作固定它:

public BindingList<Client> clientDataSource = new BindingList<Client>() { AllowNew = true }; 
+0

很高兴您找到解决方案。 – Kurubaran

+0

谢谢,我在设计器中设置了de-bindingsource的AllowNew属性,但仍然无法工作。直到我使用bindingSource的Datasource-property而不是datagridview本身的数据源属性来绑定数据。这工作。 – Peter

2

我想你错过了DataPropertyName设置。

  1. 首先,您必须将列添加到数据网格。转到添加列选项并使用向导添加所需的列。

  2. 其次,你必须编辑在向导模式中的每个列,并设置DataPropertyName。基本上,对于您创建的每个网格列,您都需要提供客户端类的确切绑定属性名称。

enter image description here


Displaying Empty Row

当您绑定一个空表到DGV默认行会被删除。如果你需要显示一个空行,然后做这样的,

//Adding a client object to the collection so that an empty row will be inserted to DGV 
clientDataSource.Add(new Client()); 
clientDataGridView.DataSource = clientDataSource; 
+0

我其实错过了其中的一列,但其余的我都有。所以现在当我删除'AutoGenerateColumns'时,我不会得到重复的列,但是当指定一个数据源时,我仍然没有得到第一个空行。我会更新问题与问题的照片:) –

+0

@Graimer检查我更新的答案。 – Kurubaran

+0

您是先生,是天才!一个微小的“问题”,但。当我这样做时,我会得到一个空白行,加上我的图片顶部的“新行”(有一颗星星)。在一个完美的世界里,我只想要那个。因为如果我使用你的解决方案,我将需要创建自定义逻辑,以避免在他们只有我创建的空白对象时按下RUN按钮。我知道我不擅长解释,但是你会遵循吗? :-) 有什么建议么? –

0

在这里看到的配置 See config here

try 
{ 

    int id =Convert.ToInt32(textBox1.Text); 
    string query = "SELECT * FROM ngo.inser WHERE ID LIKE '%" + id + "%'"; 
    command = new MySqlCommand(query, connection); 
    adapter = new MySqlDataAdapter(command); 
    table = new DataTable(); 
    adapter.Fill(table); 

    dataGridView1.DataSource = table; 
} 
catch(Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

,并转到datagridview的编辑栏设置。在具有DataPropertyName的DATA菜单中,将非属性更改为实际的数据库列名称。然后它会显示你的列信息。如果您有多个列,请设置所有这些信息。