阅读背景:

如何从DataTable绑定Enum以赢取表单CheckedListBox

来源:互联网 

i have this scenario:

我有这种情况:

[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);
[Flags]
enum 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: