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?
3 Answers. Show activity on this post. var source = new BindingSource(); List<MyStruct> list = new List<MyStruct> { new MyStruct("fff", "b"), new MyStruct("c","d") }; source. DataSource = list; grid.
If you want simple data display then a ListView may be better. If you want data-binding and complex functionality using a DataGridView is better. The Windows Forms ListView control displays a list of items with icons. You can use a list view to create a user interface like the right pane of Windows Explorer.
Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.
The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces: The IList interface, including one-dimensional arrays. The IListSource interface, such as the DataTable and DataSet classes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With