Home » Microsoft TechnologiesRSS

WinForm Gridview Column Data Source as Enum

Hello Masters,

In C# Winforms how can i have a Gridview Datacolum Datasource be set as Enum, Its Values member as Enum value and its display member as enum key/Text

 

 

5 Answers Found

 

Answer 1

I have an example of how to bind a combobox to an enum  set of values  here:

http://msmvps.com/blogs/deborahk/archive/2009/07/10/enum-binding-to-the-description-attribute.aspx

Hope this helps.

 

Answer 2

Following link contains code to set enum  as DataSource

http://www.codeproject.com/KB/cs/enumdatabinding.aspx

Or you can try following code

enum MyEnum
    {
      First =1,
      Second =2,
      Third=3
    }

    private void PopulateControl()
    {
      Array a = Enum.GetValues(typeof(MyEnum));

      DataTable dt = new DataTable();
      dt.Columns.Add("Key");
      dt.Columns.Add("Value");

      for (int i = 0; i <= a.Length - 1; i++)
      {
        int value = (int)Enum.Parse(typeof(MyEnum), a.GetValue(i).ToString(), true);
        dt.Rows.Add(value.ToString(), a.GetValue(i));
      }

      controlName.DataSource = dt;
      controlName.DisplayMember = "Value";
      controlName.ValueMember = "Key";
     
    }

Gaurav Khanna
 

Answer 3

Hi sushil,

Here has an article on MSDN guide on How to: Bind Objects to Windows Forms DataGridView Controls. It gives an example illustrates  how to display  a property with an enumeration type in a DataGridViewComboBoxColumn so that the combo box drop-down list contains the enumeration values.

Hope it helps.

Best Wishes,
Helen Zhou


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
 

Answer 4

Hi sushil,

Here has an article on MSDN guide on How to: Bind Objects to Windows Forms DataGridView Controls . It gives an example illustrates  how to display  a property with an enumeration type in a DataGridViewComboBoxColumn so that the combo box drop-down list contains the enumeration values.

Hope it helps.

Best Wishes,
Helen Zhou


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

Thanks Helen,

actually i wanted combobox to show the key and store its value to backend table, i.e. suppose i want to stores 0 for Male, 1 For Female, I want the user to see Male,Female from ____ enum  but in the data  table it will store 0 or respectively, or while navigating the record for vale 0 ____ should be shown on grid etc.

 

 

 

Answer 5

Hi sushil,

I think this should be done with DataTable which has two columns : ID and Gender. The following demo shows how to get comboBox Display member,Value Member

Following is to get combobox Display member,Value Member,you may adjust them into DataGridViewComboBoxCell. 

DataTable dataTable = new DataTable("Country");
dataTable.Columns.Add("Id");
dataTable.Columns.Add("Name");
dataTable.Rows.Add(45, "Denmark");
dataTable.Rows.Add(63, "Philippines");

comboBox1.DataSource = dataTable;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";

comboBox1.SelectedIndex = 1;
comboBox1.Refresh();

DataRow selectedDataRow = ((DataRowView)comboBox1.SelectedItem).Row;
int countryId = Convert.ToInt32(selectedDataRow["Id"]);
string countryName = selectedDataRow["Name"].ToString();

int selectedValue = Convert.ToInt32(comboBox1.SelectedValue);

Or you may refer to How to get the valuemember from a DataGridViewComboBoxCell      

http://social.msdn.microsoft.com/Forums/en-ZA/vssmartdevicesvbcs/thread/60d7e4bf-8b56-4a27-8469-ef366a992c2a  

Sincerely,
Helen Zhou

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter