【问题标题】:Bind datagridview to List<Dictionary<string, string>>将 datagridview 绑定到 List<Dictionary<string, string>>
【发布时间】:2014-06-16 09:20:01
【问题描述】:

如何将 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          
...       

【问题讨论】:

    标签: c# dictionary datagridview bind


    【解决方案1】:

    字典本身就是列表(读取集合)。但是 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);
    

    【讨论】:

    • 在我的WinForm中我添加了一个按钮,功能是:private void btnPanierAjouter_Click(object sender, EventArgs e){ //your code(without class foo) dataGridView1.DataSource=lst; } 当我第一次点击它时,结果被添加到dataGridView1,但是当我再次点击它时没有任何变化,为什么?
    • 我添加了这段代码和它的 okey `BindingSource bs = new BindingSource(); bs.DataSource = lst; dataGridView1.DataSource = bs;`认为
    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多