How to display enum values in datagridview column

后端 未结 5 986
臣服心动
臣服心动 2021-01-18 02:30

I have this Database, not of my design but I have to work with it, that contains a table like so :

 id  |   Name     |  status  | ...
-----+------------+-------         


        
5条回答
  •  失恋的感觉
    2021-01-18 03:14

    I wouldn't do it on CellFormatting. I would attack the DataTable itself. I would add a row that has the type of the enum, and the loop through the table and add the values. Something like this:

        private void Transform(DataTable table)
        {
            table.Columns.Add("EnumValue", typeof(SomeEnum));
            foreach (DataRow row in table.Rows)
            {
                int value = (int)row[1]; //int representation of enum
                row[2] = (SomeEnum)value;
            }
        }
    

    Then, in your DataGridView just hide the column that has the integer representation of your enum.

提交回复
热议问题