2016-12-12 30 views
0

我是DataGridView的新手,我試圖使用List作爲DataSource。使用列表<Item>作爲GridView中的數據源

用我目前的代碼,我的網格向我展示了3行內含空數據。有人能告訴我這有什麼不對嗎?

這裏我的代碼:

項目

class Item 
{ 
    public int id; 
    public string name; 
    public string imagePath; 
    public int type; 
    public int hp; 
    public int mp; 
    public int str; 
    public int dex; 
    public int vit; 
    public int agi; 
    public int iInt; 
    public int mnd; 
    public int att; 
    public int acc; 
    public int def; 
    public int eva; 
    public int matt; 
    public int macc; 
    public string text; 

    public Item() 
    { 

    } 

    public Item(int Id, string Name, string ImagePath, int STR, int DEX, int VIT, int AGI, int INT, int MND, 
       int ATT, int ACC, int DEF, int EVA, int MATT, int MACC, int HP, int MP, int Type, string Text) 
    { 
     id = Id; 
     name = Name; 
     imagePath = ImagePath; 
     type = Type; 
     str = STR; 
     dex = DEX; 
     vit = VIT; 
     agi = AGI; 
     iInt = INT; 
     mnd = MND; 
     att = ATT; 
     acc = ACC; 
     def = DEF; 
     eva = EVA; 
     matt = MATT; 
     macc = MACC; 
     text = Text; 
    } 

這裏我的表單代碼:

datagridItems.Rows.Clear(); 

     List<Item> items = new List<Item>(); 

     Connection connection = new Connection(); 
     string requeteItems = "SELECT * FROM Items"; 

     connection.Open(); 
     SqlDataReader myReader = connection.Read(requeteItems); 

     while (myReader.Read()) 
     { 
      Item item = new Item(); 

      item.id = int.Parse(myReader["Id"].ToString()); 
      item.name = myReader["Name"].ToString(); 
      item.imagePath = myReader["Image"].ToString(); 
      item.hp = int.Parse(myReader["HP"].ToString()); 
      item.mp = int.Parse(myReader["MP"].ToString()); 
      item.str = int.Parse(myReader["STR"].ToString()); 
      item.dex = int.Parse(myReader["DEX"].ToString()); 
      item.vit = int.Parse(myReader["VIT"].ToString()); 
      item.agi = int.Parse(myReader["AGI"].ToString()); 
      item.iInt = int.Parse(myReader["INT"].ToString()); 
      item.mnd = int.Parse(myReader["MND"].ToString()); 
      item.att = int.Parse(myReader["ATT"].ToString()); 
      item.acc = int.Parse(myReader["ACC"].ToString()); 
      item.def = int.Parse(myReader["DEF"].ToString()); 
      item.eva = int.Parse(myReader["EVAS"].ToString()); 
      item.matt = int.Parse(myReader["MATT"].ToString()); 
      item.macc = int.Parse(myReader["MACC"].ToString()); 
      item.text = myReader["Text"].ToString(); 
      item.type = 0; 
      items.Add(item); 

     } 

     connection.Close(); 

     bsItems.DataSource = items; 
    } 

連接返回DATAS沒有問題。我想知道爲什麼我的線路有空白數據。

+1

把你'Item'類字段通過更換'性能;'和'{獲得;組; }' –

+0

try bsItems.DataBind() –

回答

0

試試這個,

var bindingList = new BindingList<Item>(items); 
var source = new BindingSource(bindingList, null); 
grid.DataSource = source; 
0

參見:
How to bind list to dataGridView?
Binding List<T> to DataGridView in WinForm

使用BindingList並設置列的DataPropertyName,物業。

例如:

var list = new List<Person>() 
{ 
    new Person { Name = "Joe", }, 
    new Person { Name = "Misha", }, 
}; 
var bindingList = new BindingList<Person>(list); 
var source = new BindingSource(bindingList, null); 
grid.DataSource = source; 

更多結合相關信息..

Binding Controls to Data Created at Runtime

假設你有一個代表數據記錄和這些記錄列表應顯示在對象一個數據感知控件。爲了讓這個名單被綁定到控件,您可以執行下列操作之一:

  1. 使用System.ComponentModel.BindingList <>System.Collections.Generic.List <>泛型類型創建一個列表。與列表類不同,BindingList <>類支持更改通知。綁定到此數據源時,數據感知控件將在底層數據更改時自行更新。
  2. 創建一個類封裝的記錄列表和落實的IListIListSourceITypedListIBindingList的接口這個類。下面介紹這些接口之間的區別。

首先將字段轉換爲屬性聲明,然後使用BindingList啓用控件的全部功能。

參考文獻:
grid control - how to bind grid datasource to List ?
Bind DataGrid to Generic List<> will not allow Add, Edit items in Grid
Problem with data binding to DevExpress XtraGrid

相關問題