使用清单作为DataGridView的数据源 [英] Using a list as a data source for DataGridView
问题描述
我萃取设置名称和它们的各自的值从一个配置文件的成有序字典。该字典包含键和值它们是ICollection的类。我要绑定的数据,并在一个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.
我也试图直接设置在DataGridView源到一个有序的字典集合(键或值),但也没有造成任何我想要的;列仍然空白。然而,第三列是由与列名为长度,并显示在ICollection的条目的长度。但不用说我也不想的长度,我想自己的条目。
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.
下面是code,我使用了这个问题:当窗体的加载,我加载配置文件和私有成员称为m_Settings拥有所有的键 - 值对。然后,我创建一个列表,并分别添加键和值。绑定源设置为数据后,我运行程序和两列我说都是空白。
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();
}
任何想法,我怎么能得到有序字典和显示标示为设置和值两列中的条目?我认为,名单是为DataGridViews兼容的数据源,但现在我已经开始猜测自己。
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.
任何帮助或方向大大AP preciated!我会很乐意提供,如果需要更多的信息。
Any help or direction is greatly appreciated! I'll be happy to provide more information if needed.
谢谢!
修改
下面是修改code与实施MYSTRUCT类:
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;
}
}
然而,当我设置绑定DataDource列出,没有出现在DataGridView的,它只是空的。任何人都知道为什么吗?
However, when I set the binding DataDource to list, nothing appears on the DataGridView, it's simply empty. Anyone know why?
推荐答案
首先,我不明白为什么要添加的所有键和值count次,从来没有使用的索引。
First, I don't understand why you are adding all the keys and values count times, Index is never used.
我试过了这个例子:
var source = new BindingSource();
List<MyStruct> list = new List<MyStruct> { new MyStruct("fff", "b"), new MyStruct("c","d") };
source.DataSource = list;
grid.DataSource = source;
和工作pretty很好,我得到正确的名称两列。 MYSTRUCT类型公开的绑定机制可以使用属性。
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.
这篇关于使用清单作为DataGridView的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!