簡體   English   中英

將datagridview綁定到列表 <Dictionary<string, string> &gt;

[英]Bind datagridview to List<Dictionary<string, string>>

如何將datagridview綁定到字典列表? 例:

public static List<Dictionary<string, string>> listDict = new List<Dictionary<string, string>>();
Dictionary<string, string> dictPanier = new Dictionary<string, string>();
dictPanier["ean"] = txtStockEAN.Text;//1, 2, ...
dictPanier["titre"] = txtStockTitre.Text;// titre1, titre2, ...
dictPanier["prix"] = txtStockPrix.Text;//10, 20
dictPanier["quantite"] = txtStockQuantite.Text; // 100, 200
listDict.Add(dictPanier);    

dataGridView1有4列(ean,titre,prix,quantite),必須如下所示:

ean    |     titre     |    prix    |     quantite     
__________________________________________________
1      |     titre1    |    10      |     100          
2      |     titre2    |    20      |     200          
...       

字典本身就是列表(read集合)。 但是Dictionary不是用於綁定的正確集合。 它還有其他用途。

但是你可以做

public class Foo
{        
    public int Ean { get; set; }
    public string Titre { get; set; }
    public int Prix { get; set; }
    public int Quantite { get; set; }
}

List<Foo> lst = new List<Foo>();

Foo f = new  Foo();
f.Ean = Convert.ToInt32(txtStockEAN.Text);
f.Titre = txtStockTitre.Text;
f.Prix = Convert.ToInt32(txtStockPrix.Text);
f.Quantite = Convert.ToInt32(txtStockQuantite.Text);
lst.Add(f);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM