我有這樣的場景:如何從數據表中綁定枚舉贏得形式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);
什麼,我想是某種方式結合所選顏色的DataTable中的「顏色」列到CheckedListBox的enumed值一個不錯的方法。 有可能嗎?
到目前爲止,我一直在玩網格的RowEnter事件,但這看起來很脆弱,並不是很好。
編輯: 例如,如果我在叫MYTEXT的數據表中的第4列,我可以說列綁定到這樣一個文本框中:
myTextBox.DataBindings.Add("Text", dt, "MyText");
通過排在DataGrid中值移動時文本框會自動更改,並且文本框的任何更新都會保存回數據表。 我想從checklistbox和枚舉中獲得這個功能。
我沒有問題找出選擇哪種顏色。我想要的是將這些值綁定到選中的列表框,因此我不必每次選擇datagrid中的某一行時都要執行此計算。 – 2009-06-01 10:21:48