Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

c# - Using a list as a data source for DataGridView

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 the ICollection class. I want to bind that data and display it in a DataGridView. I've tried copying the strings to arrays and displaying those arrays, but when I ran the program the columns were blank and it did not seem to be binded at all.

I've also attempted to set the DataGridView source directly to one the ordered dictionary collections (keys or values), but that also did not result in anything I wanted; the columns were still blank. However, a third column is made with the column name as "length" and displays the lengths of the entries in the ICollection. But needless to say I do not want the lengths, I want the entries themselves.

Here is the code that I am using for this question: Upon the loading of the form, I load the configuration file and a private member called m_Settings has all the key-value pairs. Then I create a list and add the keys and the values separately. After setting the binding source to 'data', I run the program and both columns I added are both blank.

    private void Form4_Load(object sender, EventArgs e)
    {
        loadconfigfile(Properties.Settings.Default.Config);
        List<Object> data = new List<Object>();
        data.Add(m_Settings.Keys);
        data.Add(m_Settings.Values);
        bindingSource1.DataSource = data;
        dataGridView1.DataSource = bindingSource1;
        dataGridView1.Refresh();
    }

Any ideas as to how I could get the ordered dictionary and display the entries in two columns labelled "Settings" and "Values"? I believe that lists were compatible DataSources for DataGridViews, but now I'm starting to second-guess myself.

Any help or direction is greatly appreciated! I'll be happy to provide more information if needed.

Thanks!

EDIT:

Here is the revised code with the implemented myStruct class:

    List<myStruct> list = new List<myStruct>();
    for(int index = 0; index < m_Settings.Count; index++)
    {
        myStruct pair = new myStruct(keys[index], values[index].ToString());
        list.Add(pair);
    }

    public class myStruct
    {
        private string Key { get; set; }
        private string Value { get; set; }

        public myStruct(string key, string value)
        {
            Key = key;
            Value = value;
        }
    }

However, when I set the binding DataDource to list, nothing appears on the DataGridView, it's simply empty. Anyone know why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...