Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- using System.Diagnostics;
- using System.Text;
- using System.Linq;
- using System.Net;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Linq.Expressions;
- using System.ComponentModel;
- using System.Data;
- namespace Test_1
- {
- public partial class Form1 : Form
- {
- private BindingSource bindingSource1 = new BindingSource();
- public Form1()
- {
- InitializeComponent();
- this.Load += new System.EventHandler(EnumsAndComboBox_Load);
- }
- private void EnumsAndComboBox_Load(object sender, System.EventArgs e)
- {
- // Populate the data source.
- bindingSource1.Add(new Knight(Title.King, "Uther", true));
- bindingSource1.Add(new Knight(Title.King, "Arthur", true));
- bindingSource1.Add(new Knight(Title.Sir, "Mordred", false));
- bindingSource1.Add(new Knight(Title.Sir, "Gawain", true));
- bindingSource1.Add(new Knight(Title.Sir, "Galahad", true));
- // Initialize the DataGridView.
- dataGridView1.AutoGenerateColumns = false;
- dataGridView1.AutoSize = true;
- dataGridView1.DataSource = bindingSource1;
- dataGridView1.Columns.Add(CreateComboBoxWithEnums());
- // Initialize and add a text box column.
- DataGridViewColumn column = new DataGridViewTextBoxColumn();
- column.DataPropertyName = "Name";
- column.Name = "Knight";
- dataGridView1.Columns.Add(column);
- // Initialize and add a check box column.
- column = new DataGridViewCheckBoxColumn();
- column.DataPropertyName = "GoodGuy";
- column.Name = "Good";
- dataGridView1.Columns.Add(column);
- // Initialize the form.
- this.Controls.Add(dataGridView1);
- this.AutoSize = true;
- this.Text = "DataGridView object binding demo";
- }
- private static T GetAttribute<T>(Enum value)
- {
- T attribute = value.GetType()
- .GetMember(value.ToString())[0].GetCustomAttributes(typeof(T), false)
- .Cast<T>()
- .SingleOrDefault();
- return attribute;
- }
- DataGridViewComboBoxColumn CreateComboBoxWithEnums()
- {
- DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
- //combo.DataSource = Enum.GetValues(typeof(Title));
- var datatable = new DataTable(); //Setup a DataTable for your ComboBox Column
- datatable.Columns.Add("col1", typeof(string));
- datatable.Columns.Add("col2", typeof(Title));
- foreach (Title item in Enum.GetValues(typeof(Title)))
- {
- datatable.Rows.Add(GetAttribute<DescriptionAttribute>(item).Description, item);
- }
- combo.DisplayMember = "col1";
- combo.ValueMember = "col2";
- combo.DataSource = datatable;
- combo.DropDownWidth = 200;
- combo.DataPropertyName = "Title";
- combo.Name = "Title";
- return combo;
- }
- private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
- {
- if (e.ColumnIndex == 0)//where Column1 is your combobox column
- {
- var val = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
- if (val != null)
- {
- e.Value = ((Title)val).ToString();
- e.FormattingApplied = true;
- }
- }
- }
- #region "business object"
- private class Knight
- {
- private string hisName;
- private bool good;
- private Title hisTitle;
- public Knight(Title title, string name, bool good)
- {
- hisTitle = title;
- hisName = name;
- this.good = good;
- }
- public Knight()
- {
- hisTitle = Title.Sir;
- hisName = "<enter name>";
- good = true;
- }
- public string Name
- {
- get
- {
- return hisName;
- }
- set
- {
- hisName = value;
- }
- }
- public bool GoodGuy
- {
- get
- {
- return good;
- }
- set
- {
- good = value;
- }
- }
- public Title Title
- {
- get
- {
- return hisTitle;
- }
- set
- {
- hisTitle = value;
- }
- }
- }
- #endregion
- }
- public enum Title
- {
- [Description("This better be thy king!")]
- King,
- [Description("aka wanna-be-king")]
- Sir
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement