问题
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?
回答1:
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.
回答2:
Set the DataGridView property
gridView1.AutoGenerateColumns = true;
And make sure the list of objects your are binding, those object properties should be public.
回答3:
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
来源:https://stackoverflow.com/questions/6473326/using-a-list-as-a-data-source-for-datagridview