-
-
Save hackedd/d088cd5a32d21964776e to your computer and use it in GitHub Desktop.
DataGridView and ICustomTypeDescriptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
namespace DataGridViewTest | |
{ | |
public class DataGridViewTest | |
{ | |
class Person | |
{ | |
public Person(string firstName, string lastName) | |
{ | |
FirstName = firstName; | |
LastName = lastName; | |
} | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
class RecordWrapper : ICustomTypeDescriptor | |
{ | |
public RecordWrapper(Person record) | |
{ | |
Object = record; | |
} | |
public Person Object { get; set; } | |
AttributeCollection ICustomTypeDescriptor.GetAttributes() | |
{ | |
return TypeDescriptor.GetAttributes(Object); | |
} | |
string ICustomTypeDescriptor.GetClassName() | |
{ | |
return typeof(Person).FullName; | |
} | |
string ICustomTypeDescriptor.GetComponentName() | |
{ | |
return TypeDescriptor.GetFullComponentName(Object); | |
} | |
TypeConverter ICustomTypeDescriptor.GetConverter() | |
{ | |
return TypeDescriptor.GetConverter(Object); | |
} | |
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() | |
{ | |
return TypeDescriptor.GetDefaultEvent(Object); | |
} | |
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() | |
{ | |
return TypeDescriptor.GetDefaultProperty(Object); | |
} | |
object ICustomTypeDescriptor.GetEditor(Type editorBaseType) | |
{ | |
return null; | |
} | |
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) | |
{ | |
return TypeDescriptor.GetEvents(Object, attributes); | |
} | |
EventDescriptorCollection ICustomTypeDescriptor.GetEvents() | |
{ | |
return TypeDescriptor.GetEvents(Object); | |
} | |
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) | |
{ | |
return TypeDescriptor.GetProperties(Object, attributes); | |
} | |
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() | |
{ | |
return TypeDescriptor.GetProperties(Object); | |
} | |
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) | |
{ | |
return Object; | |
} | |
} | |
class TestForm : Form | |
{ | |
SplitContainer splitContainer; | |
public TestForm() | |
{ | |
splitContainer = new SplitContainer(); | |
splitContainer.Orientation = Orientation.Horizontal; | |
splitContainer.Dock = DockStyle.Fill; | |
Controls.Add(splitContainer); | |
List<Person> records = new List<Person>(); | |
records.Add(new Person("Jane", "Doe")); | |
records.Add(new Person("John", "Doe")); | |
DataGridView dataGridView1 = CreateDataGridView(); | |
splitContainer.Panel1.Controls.Add(dataGridView1); | |
dataGridView1.DataSource = records; | |
List<RecordWrapper> wrappedRecords = new List<RecordWrapper>(); | |
foreach (Person record in records) | |
wrappedRecords.Add(new RecordWrapper(record)); | |
DataGridView dataGridView2 = CreateDataGridView(); | |
splitContainer.Panel2.Controls.Add(dataGridView2); | |
dataGridView2.DataSource = wrappedRecords; | |
} | |
DataGridView CreateDataGridView() | |
{ | |
DataGridView dataGridView = new DataGridView(); | |
DataGridViewTextBoxColumn columnA = new DataGridViewTextBoxColumn(); | |
DataGridViewTextBoxColumn columnB = new DataGridViewTextBoxColumn(); | |
dataGridView.Dock = DockStyle.Fill; | |
dataGridView.AutoGenerateColumns = false; | |
dataGridView.Columns.Add(columnA); | |
dataGridView.Columns.Add(columnB); | |
columnA.HeaderText = "First Name"; | |
columnA.ReadOnly = true; | |
columnA.DataPropertyName = "FirstName"; | |
columnB.HeaderText = "Last Name"; | |
columnB.DataPropertyName = "LastName"; | |
return dataGridView; | |
} | |
} | |
public static void Main() | |
{ | |
Application.Run(new TestForm()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment