如何从数据表中绑定枚举赢得形式CheckedListBox
题
我有这样的场景:
[Flags]
enum Colors : long
(
red = 1,
blue = 2,
green = 4,
yellow = 8,
)
DataTable dt = new DataTable();
dt.Columns.Add("PersonName", typeof(string));
dt.Columns.Add("CheckOption", typeof(bool));
dt.Columns.Add("Colors", typeof(long));
// note that the values in the Colors column are enumed values of chosen colors
dt.Rows.Add("Name 1", true, 1); // red
dt.Rows.Add("Name 2", true, 12); // green and yellow
dt.Rows.Add("Name 3", true, 4); // green
dt.Rows.Add("Name 4", false, 11); // red, blue and yellow
// bind the datatable to grid
DataGridView dgv = new DataGridView();
dgv.DataSource = dt;
// hide the colors in the grid
dgv.Columns["Colors"].Visible = false;
// checked list box has all items from the enum
CheckedListBox clb = new CheckedListBox();
string[] colorsArray = Enum.GetNames(typeof(Colors));
clb.Items.AddRange(colorsArray);
我想是所选择的颜色的数据表中的“颜色”列到CheckedListBox的enumed值以某种方式结合在一个很好的方式。 这可能吗?
到目前为止,我一直在玩电网的RowEnter事件,但是这似乎很脆,而不是真的很好的。
编辑: 例如,如果我在称为MYTEXT的数据表中的第4列我能该列结合到这样一个文本框:
myTextBox.DataBindings.Add("Text", dt, "MyText");
通过中的行数据网格中的文本框中的值automaticaly改变移动和文本框的任何更新被保存回数据表时。 我想获得从checklistbox和枚举过此functionalty。
解决方案
有没有简单的方法来做到这一点,因为这不是典型的主从场景。我想你应该开始毕竟书面方式代码。
其他提示
我不完全相信你以后,但你可以使用一个贪心算法来确定正在使用哪些颜色?
例如,你有数量11的唯一可能的方式来获得11通过具有黄色,所以你删除适合(8)的最大数目,并检查相应的复选框。然后你得到3.适合为2(蓝色)最大数,那么其余是显而易见的。
不隶属于 StackOverflow