使用列表作为 DataGridView 的数据源
我已将配置文件中的设置名称及其各自的值提取到有序字典中。该字典包含 ICollection 类的键和值。我想绑定该数据并将其显示在 DataGridView 中。我尝试将字符串复制到数组并显示这些数组,但是当我运行程序时,列是空白的,并且似乎根本没有绑定。
我还尝试将 DataGridView 源直接设置为有序字典集合(键或值)之一,但这也没有得到我想要的任何结果;这些列仍然是空白的。但是,第三列的列名称为“length”,并显示 ICollection 中条目的长度。但不用说,我不想要长度,我想要条目本身。
这是我用于解决此问题的代码:加载表单后,我加载配置文件,并且名为 m_Settings 的私有成员拥有所有键值对。然后我创建一个列表并分别添加键和值。将绑定源设置为“数据”后,我运行该程序,我添加的两列都是空白的。
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();
}
关于如何获取有序字典并在标有“设置”和“值”的两列中显示条目的任何想法?我相信列表是 DataGridView 的兼容数据源,但现在我开始怀疑自己。
非常感谢任何帮助或指导!如果需要,我很乐意提供更多信息。
谢谢!
编辑:
这是修改后的代码,已实现 myStruct 类:
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 上什么也没有出现,它只是空的。有人知道为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我不明白为什么您要添加所有键和值计数次数,而从未使用索引。
我尝试了这个例子:
效果很好,我得到了两列名称正确的列。 MyStruct 类型公开绑定机制可以使用的属性。
尝试构建一种采用一个键和值的类型,并将其一一添加。
希望这有帮助。
First, I don't understand why you are adding all the keys and values count times, Index is never used.
I tried this example :
and that work pretty well, I get two columns with the correct names. MyStruct type exposes properties that the binding mechanism can use.
Try to build a type that takes one key and value, and add it one by one.
Hope this helps.
设置 DataGridView 属性
并确保您要绑定的对象列表,这些对象属性应该是公共的。
Set the DataGridView property
And make sure the list of objects your are binding, those object properties should be public.
这个功能可能会帮助你。它将每个列表对象添加到网格视图
我为简单的数据库应用程序编写了这个
this Func may help you . it add every list object to grid view
I write this for simple database app