DataGrid - How to get name of selected cell's column heading name?
Hi forum,
I am trying to design some generic forms that can be used to dynamically bind to any type of business object.
At the moment I am binding a System.Windows.Forms.DataGrid to an ArrayList of custom business objects. And I am not using any DataGridTableStyle. So that means for every public property in my class there is a column in the grid. This is great, but how am I able to get the column heading name of a selected cell so that I may perform some reflection on the selected object/property? I need the column heading name which is the same as the property name. How do I get the column heading name?
Am unable to find any documentation on how to do this.
Help! Please, Thanking in advance for any help greatly appreciated.

schangtum5 reputationPosted Jan 20, 2004, 4:44 AM EST
public ObjectViewForm(Object datasource) { InitializeComponent(); if(datasource is IList) dgObjects.DataSource = datasource; else { ArrayList list = new ArrayList(); list.Add(datasource); dgObjects.DataSource = list; DataGridTableStyle dgstyle = new DataGridTableStyle(); dgstyle.MappingName = "ArrayList"; dgObjects.TableStyles.Add(dgstyle); } } private void btnCreate_Click(object sender, System.EventArgs e) { //this button performs a property assignment, assinging a new instance IList list = (IList)dgObjects.DataSource; Object obj = list[dgObjects.CurrentRowIndex]; string name = dgObjects.TableStyles[0].GridColumnStyles[dgObjects.CurrentCell.ColumnNumber].MappingName; Type type = obj.GetType(); PropertyInfo property = type.GetProperty(name); Object detail = Activator.CreateInstance(property.PropertyType, null); property.SetValue(obj, detail, null); } private void btnNullify_Click(object sender, System.EventArgs e) { //this button performs a property assignment, assigning NULL IList list = (IList)dgObjects.DataSource; Object obj = list[dgObjects.CurrentRowIndex]; string name = dgObjects.TableStyles[0].GridColumnStyles[dgObjects.CurrentCell.ColumnNumber].MappingName; Type type = obj.GetType(); PropertyInfo property = type.GetProperty(name); property.SetValue(obj, null, null); }