[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?

The answer is


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


Set the DataGridView property

    gridView1.AutoGenerateColumns = true;

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


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to datagridview

How to refresh or show immediately in datagridview after inserting? Delete a row in DataGridView Control in VB.NET Looping each row in datagridview How to get cell value from DataGridView in VB.Net? Changing datagridview cell color based on condition Index was out of range. Must be non-negative and less than the size of the collection parameter name:index how to bind datatable to datagridview in c# DataGridView AutoFit and Fill How to export dataGridView data Instantly to Excel on button click? Populate a datagridview with sql query results

Examples related to datasource

Spring Boot Configure and Use Two DataSources Configure DataSource programmatically in Spring Boot Unable to find the requested .Net Framework Data Provider in Visual Studio 2010 Professional How to use JNDI DataSource provided by Tomcat in Spring? Using a list as a data source for DataGridView Binding Combobox Using Dictionary as the Datasource How to connect to a MySQL Data Source in Visual Studio VB.NET: Clear DataGridView c# dictionary one key many values How do I manually configure a DataSource in Java?

Examples related to icollection

Why use ICollection and not IEnumerable or List<T> on many-many/one-many relationships? Using a list as a data source for DataGridView

Examples related to ordereddictionary

How to convert an OrderedDict into a regular dict in python3 Rename a dictionary key Converting dict to OrderedDict Accessing items in an collections.OrderedDict by index Can I get JSON to load into an OrderedDict? Using a list as a data source for DataGridView