Using a list as a data source for DataGridView

前端 未结 3 1593
迷失自我
迷失自我 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:01

    Set the DataGridView property

        gridView1.AutoGenerateColumns = true;
    

    And make sure the list of objects your are binding, those object properties should be public.

    0 讨论(0)
  • 2020-11-30 06:10

    this Func may help you . it add every list object to grid view

    private void show_data()
            {
                BindingSource Source = new BindingSource();
    
                for (int i = 0; i < CC.Contects.Count; i++)
                {
                    Source.Add(CC.Contects.ElementAt(i));
                };
    
    
                Data_View.DataSource = Source;
    
            }
    

    I write this for simple database app

    0 讨论(0)
  • 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<MyStruct> list = new List<MyStruct> { 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.

    0 讨论(0)
提交回复
热议问题