开发者

Binding DataGridViewCheckBoxColumn to array of booleans

开发者 https://www.devze.com 2023-02-27 15:07 出处:网络
I\'m currently trying to build a DataGridView in a Windows Form to display to a user a list of settings that they can turn on and off.The DatGridView will have two columns, the fir开发者_开发问答st co

I'm currently trying to build a DataGridView in a Windows Form to display to a user a list of settings that they can turn on and off. The DatGridView will have two columns, the fir开发者_开发问答st column will describe the setting and the second column will containg a check box allowing the user to turn the setting on or off. So for example the DataGridView would look like:

| Descriptions        |   Set   |
---------------------------------
| Description 1       |  true   |
| Description 2       |  false  |
| Description 3       |  false  |
...

In my project settings I currently have a list of booleans, Pref1, Pref2, Pref3 etc which I'd like to bind to the CheckBoxes within the DataGridView so that they can be manipulated automatically without me having to do any manual checks whenever a cells value has been edited and so I can save the changes between different instances of the application.

I tried searching for a few solutions and came across the following and added it to the forms constructor:

// Build preference dictionary
Dictionary<String, bool> Preferences = new Dictionary<String, bool>();
preferences.Add("Description 1", Settings.Default.Pref1);
preferences.Add("Description 2", Settings.Default.Pref2);
....

// Copy dictionary to list
List<KeyValuePair<String, bool>> PreferenceList = new List<KeyValuePair<String, bool>>();
foreach (KeyValuePair<String, bool> item in Preferences)
    PreferenceList.Add(item);

// Set the GridView DataSource and values displayed in each column
GridView.AutoGenerateColuns = false;
GridView.DataSource = new BindingList<KeyValuePair<String, bool>>(PreferenceList);
GridView.Columns[0].DataPropertyName = "Key";
GridView.Columns[1].DataPropertyName = "Value";

When the form loads the DataGridView populates as expected but the CheckBoxes in the second column can't be manipulated. After some debugging I've notcied the second column becomes ReadOnly when I set the DataPropertyName to "Value" and I can't change this ReadOnly setting without throwing an exception.

Is there any way around this readonly issue? I've also read and thought about creating my own Preference class something along the lines of:

public Class Preference
{
    public String Description  { get, set }
    public bool Selected  { get, set }
}

And then creating an array of these preferences, binding each Preference's 'Selected' property to one of the booleans in my Settings class and then setting the Preference array as the DataGridViews DataSource. Is this a viable solution or is there maybe another alternative that I'm not considering/aware of?

Sorry for the essay of a question but I just like to try and explain everything so theres no confusion in what I'm asking :)

Thanks in advance.


GridView.DataSource = Preferences 
   .Select(p => new Preference {Description = p.Key, Selected = p.Value})
   .ToList();
GridView.Columns[0].DataPropertyName = "Description";
GridView.Columns[1].DataPropertyName = "Selected";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号