How to show Enum type members in a DataGridViewComboBox?

后端 未结 4 969
醉酒成梦
醉酒成梦 2021-02-03 10:30

What else I have to do in order to show ReadAccess enum members in this DatagridViewComboBox?

ReadDataGridViewComboBoxColumn.Items.Clear();
ReadDa         


        
4条回答
  •  萌比男神i
    2021-02-03 11:29

    An Improvement to accepted answer: There is no need to type enum members as array manually. Instead you can use System.Enum.GetValues(typeof(ReadAccess)). Also, instead of anonymous type list, you can use a List from Dictionary (Dictionary isn't accepted as datasource):

    ReadDataGridViewComboBoxColumn.DataSource= System.Enum.GetValues(typeof(ReadAccess))
      .Cast.ToDictionary((e) => e.ToString(), (e) =>  e).ToList;
    

    or directly a KeyValuePair list :

    ReadDataGridViewComboBoxColumn.DataSource = System.Enum.GetValues(typeof(ReadAccess))
      .Cast.Select((value) => new KeyValuePair(value.ToString(), (value)));
    

    Still necessary (but DisplayMember now is "Key") :

     ReadDataGridViewComboBoxColumn.ValueType = typeof(ReadAccess);
     ReadDataGridViewComboBoxColumn.ValueMember = "Value";
     ReadDataGridViewComboBoxColumn.DisplayMember = "Key";
    

提交回复
热议问题