DataTable에서 Enum을 바인딩하는 방법 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의 "Colors"열에서 선택한 색상의 열거 된 값을 CheckedListbox에 좋은 방법으로 바인딩하는 것입니다. 가능합니까?
지금까지 나는 그리드의 Rowenter 이벤트를 가지고 놀았지만 이것은 매우 부서지기 쉬운 것처럼 보이고 전혀 좋지 않습니다.
편집 : 예를 들어 MyText라는 데이터 가능에 4 번째 열이있는 경우 해당 열이 다음과 같은 텍스트 상자에 바인딩 할 수 있습니다.
myTextBox.DataBindings.Add("Text", dt, "MyText");
DataGrid의 행을 이동할 때 TextBox Automaticaly 변경 사항의 값이 변경되고 TextBox에 대한 모든 업데이트가 DataTable에 다시 저장됩니다. Checklistbox와 Enums 에서도이 기능을 얻고 싶습니다.
해결책
일반적인 마스터 세부 시나리오가 아니기 때문에 쉽게 수행 할 수있는 방법은 없습니다. After After After에서 그 코드를 작성하기 시작해야한다고 생각합니다.
다른 팁
당신이 무엇을 추구하는지 잘 모르겠지만 탐욕스러운 알고리즘을 사용하여 어떤 색상이 사용되는지 결정할 수 있습니까?
예를 들어, 당신은 숫자 11이 있습니다. 11을 얻을 수있는 유일한 방법은 노란색을 갖는 것입니다. 따라서 (8)에 맞는 가장 큰 숫자를 제거하고 해당 확인란을 확인하십시오. 그런 다음 3. 가장 큰 숫자는 2 (파란색)입니다. 그러면 나머지는 분명합니다.
제휴하지 않습니다 StackOverflow