Advertisement
Guest User

Untitled

a guest
Apr 4th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Windows.Forms.DataVisualization.Charting;
  5. using System.Diagnostics;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Collections.Generic;
  10. using System.Reflection;
  11. using System.Linq.Expressions;
  12. using System.ComponentModel;
  13. using System.Data;
  14.  
  15. namespace Test_1
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         private BindingSource bindingSource1 = new BindingSource();
  20.  
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.  
  26.             this.Load += new System.EventHandler(EnumsAndComboBox_Load);
  27.  
  28.         }
  29.  
  30.         private void EnumsAndComboBox_Load(object sender, System.EventArgs e)
  31.         {
  32.             // Populate the data source.
  33.             bindingSource1.Add(new Knight(Title.King, "Uther", true));
  34.             bindingSource1.Add(new Knight(Title.King, "Arthur", true));
  35.             bindingSource1.Add(new Knight(Title.Sir, "Mordred", false));
  36.             bindingSource1.Add(new Knight(Title.Sir, "Gawain", true));
  37.             bindingSource1.Add(new Knight(Title.Sir, "Galahad", true));
  38.  
  39.             // Initialize the DataGridView.
  40.             dataGridView1.AutoGenerateColumns = false;
  41.             dataGridView1.AutoSize = true;
  42.             dataGridView1.DataSource = bindingSource1;
  43.  
  44.             dataGridView1.Columns.Add(CreateComboBoxWithEnums());
  45.  
  46.             // Initialize and add a text box column.
  47.             DataGridViewColumn column = new DataGridViewTextBoxColumn();
  48.             column.DataPropertyName = "Name";
  49.             column.Name = "Knight";
  50.             dataGridView1.Columns.Add(column);
  51.  
  52.             // Initialize and add a check box column.
  53.             column = new DataGridViewCheckBoxColumn();
  54.             column.DataPropertyName = "GoodGuy";
  55.             column.Name = "Good";
  56.             dataGridView1.Columns.Add(column);
  57.  
  58.             // Initialize the form.
  59.             this.Controls.Add(dataGridView1);
  60.             this.AutoSize = true;
  61.             this.Text = "DataGridView object binding demo";
  62.         }
  63.  
  64.         private static T GetAttribute<T>(Enum value)
  65.         {
  66.             T attribute = value.GetType()
  67.                 .GetMember(value.ToString())[0].GetCustomAttributes(typeof(T), false)
  68.                 .Cast<T>()
  69.                 .SingleOrDefault();
  70.             return attribute;
  71.         }
  72.  
  73.         DataGridViewComboBoxColumn CreateComboBoxWithEnums()
  74.         {
  75.             DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
  76.             //combo.DataSource = Enum.GetValues(typeof(Title));        
  77.  
  78.  
  79.             var datatable = new DataTable(); //Setup a DataTable for your ComboBox Column
  80.             datatable.Columns.Add("col1", typeof(string));
  81.             datatable.Columns.Add("col2", typeof(Title));
  82.  
  83.             foreach (Title item in Enum.GetValues(typeof(Title)))
  84.             {
  85.                 datatable.Rows.Add(GetAttribute<DescriptionAttribute>(item).Description, item);
  86.             }
  87.  
  88.             combo.DisplayMember = "col1";
  89.             combo.ValueMember = "col2";
  90.             combo.DataSource = datatable;
  91.  
  92.             combo.DropDownWidth = 200;
  93.  
  94.             combo.DataPropertyName = "Title";
  95.             combo.Name = "Title";
  96.             return combo;
  97.         }
  98.  
  99.         private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  100.         {
  101.             if (e.ColumnIndex == 0)//where Column1 is your combobox column
  102.             {
  103.                 var val = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
  104.                 if (val != null)
  105.                 {
  106.                     e.Value = ((Title)val).ToString();
  107.                     e.FormattingApplied = true;
  108.                 }
  109.                
  110.             }
  111.         }
  112.  
  113.         #region "business object"
  114.         private class Knight
  115.         {
  116.             private string hisName;
  117.             private bool good;
  118.             private Title hisTitle;
  119.  
  120.             public Knight(Title title, string name, bool good)
  121.             {
  122.                 hisTitle = title;
  123.                 hisName = name;
  124.                 this.good = good;
  125.             }
  126.  
  127.             public Knight()
  128.             {
  129.                 hisTitle = Title.Sir;
  130.                 hisName = "<enter name>";
  131.                 good = true;
  132.             }
  133.  
  134.             public string Name
  135.             {
  136.                 get
  137.                 {
  138.                     return hisName;
  139.                 }
  140.  
  141.                 set
  142.                 {
  143.                     hisName = value;
  144.                 }
  145.             }
  146.  
  147.             public bool GoodGuy
  148.             {
  149.                 get
  150.                 {
  151.                     return good;
  152.                 }
  153.                 set
  154.                 {
  155.                     good = value;
  156.                 }
  157.             }
  158.  
  159.             public Title Title
  160.             {
  161.                 get
  162.                 {
  163.                     return hisTitle;
  164.                 }
  165.                 set
  166.                 {
  167.                     hisTitle = value;
  168.                 }
  169.             }
  170.         }
  171.         #endregion
  172.  
  173.     }
  174.  
  175.     public enum Title
  176.     {
  177.         [Description("This better be thy king!")]
  178.         King,
  179.  
  180.         [Description("aka wanna-be-king")]
  181.         Sir
  182.     };
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement