2014-05-07 42 views
1

因此,我需要在網格中顯示人們輸入的數字列表,以幫助他們仔細檢查他們的工作。它幾乎可以工作,但它不顯示數字。我的設置很簡單。我有一個文本框,他們輸入數字,當添加按鈕被點擊時,它被添加到一個BindingList中,然後被用作DataGridView的數據源。Winforms如何綁定到列表<long> DatagridView並使其正確顯示

所以,在Stackoverflow Post的幫助下,我能夠完成這一半的工作。 不幸的是,即使它每次沒有正確顯示值時都會添加一行Grid。 它顯示新行爲空。

這是我的代碼。

public partial class ManualEntry : Form 
    { 

    BindingList<long> ProjectIDs; 
    public ManualEntry() 
    { 
     InitializeComponent(); 
     ProjectIDs = new BindingList<long>(); 
    } 

單擊添加按鈕時,會執行此操作。

 private void AddButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       long temp = long.Parse(textBox1.Text); 
       ProjectIDs.Add(temp); 
       ProjectsGrid.DataSource = ProjectIDs; 
       textBox1.Text = "";//clear the textbox so they can add a new one. 
      } 
      catch//bring up the badinput form 
      { 
       BadInput b = new BadInput(); 
       b.Show(); 
      } 

     } 

所以這裏是添加幾個數字的結果。

add project

如果你需要從我這裏任何其他代碼,以幫助您回答這個問題,只是問。

+0

我也嘗試使用一個綁定源作爲DataGridView的數據源,但我最終得到了相同的結果。 –

回答

3

你還沒有告訴什麼樣的DataGridViewColumn綁定到。

通常你綁定到綁定數據類型的公共屬性。在這種情況下,您的數據類型爲long,它沒有適當的屬性進行綁定。

將您的long包裝在自定義類中,並將其作爲公共屬性公開。

public class Data 
{ 
    public long Value { get; set; } 
} 

您綁定列到Value財產。您可以在設計做到這一點,但這裏是代碼:

Column1.DataPropertyName = "Value"; 

現在不是long您使用Data

ProjectIDs = new BindingList<Data>(); 

...

long temp = long.Parse(textBox1.Text); 
ProjectIDs.Add(new Data { Value = temp }); 
+0

這聽起來像是一種不必要的複雜方式來實現這一結果。你能否展示一種手動添加DataGridViewRow的方法。如果我嘗試DataGridViewRow r = new DataGridViewRow();我得到一個索引超出範圍異常。 r.Cells [0] .Value = temp.ToString(); ProjectsGrid.Rows.Add(r); –

+0

您在嘗試此操作之前是否添加了datagridviewcolumn? 像這樣:dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()); – Derek

+2

@AlexanderRyanBaggett,請記住,DataGridView旨在顯示網格中對象(=一行)的多個信息(=屬性)。它不只是另一個ListBox。如果你只想顯示一個列,你可能想要使用一個列表框。只要你想顯示兩列或更多的列,它不會出現一個不必要的複雜的方式了... – elgonzo

1

以下後討論這個:

DataGridView bound to BindingList does not refresh when value changed

它看起來像綁定列表中的數據類型,需要支持INotifyPropertyChanged接口:

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

注:修訂 - 我使用INotifyPropertyChanged是不正確的,似乎也不需要。 現在,這個答案基本上是一樣Igby的 - 所以我認爲他是最好的一個:)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
     InitializeComponent(); 
     ProjectIDs = new BindingList<AwesomeLong>(); 
     var source = new BindingSource(ProjectIDs, null); 
     dataGridView1.DataSource = source; 
     dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()); 
     } 

     BindingList<AwesomeLong> ProjectIDs; 
     private int i = 0; 
     private void button1_Click(object sender, EventArgs e) 
     { 
     i++; 
     ProjectIDs.Add(new AwesomeLong(i)); 
     } 

    } 

    public class AwesomeLong 
    { 
     public long LongProperty { get; set; } 

     public AwesomeLong(long longProperty) 
     { 
     LongProperty = longProperty; 
     }  
    } 
} 
相關問題