将集合绑定到DataGridView

时间:2014-10-07 12:08:49

标签: c# winforms datagridview

我试图将我的绑定列表绑定到DataGridView,但并不是所有内容都显示出来!这是我目前拥有的代码:

public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int[] numbers = new int[6] 
}

public class Data
{
    public static BindingList<Person> persons = new BindingList<Person>(); 
}

...

var bl = new BindingList<Person>(persons);
myGrid.DataSource = bl;

除了Person.numbers未在DataGridView中显示(仅ID,FName&amp; LName)之外,一切都很有效!

任何想法为什么会这样?

1 个答案:

答案 0 :(得分:1)

我会尝试将其作为财产。编辑:要格式化以在datagridview中使用,您可以使用setter并将列绑定到格式化字符串,如下例所示。

public class Person
{
  public int ID { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public String NumbersString { get; set; }

  private int[] _numbers;
  public int[] Numbers
    {
         get
             {
                return _numbers;
             }
         set
            {
              _numbers = value;
              NumbersString = FormatIntoString(_numbers);

            }

     }

  public Person()
  {
  numbers = new int[6];

  }


 // The write the method that puts the array into a readable form

 private string FormatIntoString(int[] array)
   {
     string result = "";

     foreach(var x in array)
      {
        result += x.ToString() + ",";
      }
     return result;
   }
相关问题