Using a list as a data source for DataGridView

前端 未结 3 1608
迷失自我
迷失自我 2020-11-30 05:50

I\'ve extracted the setting names and their respective values out of a configuration file into an ordered dictionary. The dictionary contains keys and values which are of th

3条回答
  •  情歌与酒
    2020-11-30 06:28

    First, I don't understand why you are adding all the keys and values count times, Index is never used.

    I tried this example :

            var source = new BindingSource();
            List list = new List { new MyStruct("fff", "b"),  new MyStruct("c","d") };
            source.DataSource = list;
            grid.DataSource = source;
    

    and that work pretty well, I get two columns with the correct names. MyStruct type exposes properties that the binding mechanism can use.

        class MyStruct
       {
        public string Name { get; set; }
        public string Adres { get; set; }
    
    
        public MyStruct(string name, string adress)
        {
            Name = name;
            Adres = adress;
        }
      }
    

    Try to build a type that takes one key and value, and add it one by one. Hope this helps.

提交回复
热议问题