2013-12-14 84 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菜單中,將非屬性更改爲實際的數據庫列名稱。然後它會顯示你的列信息。如果您有多個列,請設置所有這些信息。