Getting ColumnName from BoundDataGrid

Hi, Could anyone shed me some light on how i can retrieve information about what column (column name) i am looking at when i click on a BoundDataGrid?

8 Replies

SA Satish April 19, 2004 10:53 AM UTC

Use the following code to get the columnName. private void gridDataBoundGrid1_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e) { GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell; int field = this.gridDataBoundGrid1.Binder.ColIndexToField(cc.ColIndex); string mappingName = this.gridDataBoundGrid1.Binder.InternalColumns[field].MappingName; MessageBox.Show(mappingName); } Thanks Satish >Hi, > >Could anyone shed me some light on how i can retrieve information about what column (column name) i am looking at when i click on a BoundDataGrid?


VL Vincent Lo April 19, 2004 11:39 AM UTC

Thanks! what should i do if i have got 2 GridHierarchyLevel of data? How can i recogize which hierarchy does that row belongs to? Since the dataGrid''s binder only binds to root hierarchy?


AD Administrator Syncfusion Team April 19, 2004 10:09 PM UTC

You can get the GridBoundRecordState object for the given row using GridBoundRecordState rs = this.grid.Binder.GetRecordStateAtRowIndex(rowIndex); The GridBoundRecordState has a LevelIndex property.


VL Vincent Lo April 20, 2004 06:21 AM UTC

Thanks, I am able to know which hierarchy i am in but how i can get the underlying dataRow from this info? In my cell_click I have got this piece of code: CurrencyManager cm; GridBoundRecordState rs = gridBinder.GetRecordStateAtRowIndex(e.RowIndex); cm = (CurrencyManager) this.BindingContext[this.gridDataBoundGrid1.DataSource, this.dataSet.Tables[rs.LevelIndex].TableName]; DataRow dr = ((DataRowView)cm.Current).Row; GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell; int field = this.gridDataBoundGrid1.Binder.ColIndexToField(cc.ColIndex); string mappingName; if (rs.LevelIndex == 0) { mappingName = ghlMkt.GridBoundColumns[field].MappingName; } else { mappingName = ghlMktSub.GridBoundColumns[field].MappingName; } Console.WriteLine(mappingName+" "+ dr[mappingName].ToString()); >You can get the GridBoundRecordState object for the given row using > >GridBoundRecordState rs = this.grid.Binder.GetRecordStateAtRowIndex(rowIndex); > > >The GridBoundRecordState has a LevelIndex property.


VL Vincent Lo April 20, 2004 06:21 AM UTC

Thanks, I am able to know which hierarchy i am in but how i can get the underlying dataRow from this info? In my cell_click I have got this piece of code: CurrencyManager cm; GridBoundRecordState rs = gridBinder.GetRecordStateAtRowIndex(e.RowIndex); cm = (CurrencyManager) this.BindingContext[this.gridDataBoundGrid1.DataSource, this.dataSet.Tables[rs.LevelIndex].TableName]; DataRow dr = ((DataRowView)cm.Current).Row; GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell; int field = this.gridDataBoundGrid1.Binder.ColIndexToField(cc.ColIndex); string mappingName; if (rs.LevelIndex == 0) { mappingName = ghlMkt.GridBoundColumns[field].MappingName; } else { mappingName = ghlMktSub.GridBoundColumns[field].MappingName; } Console.WriteLine(mappingName+" "+ dr[mappingName].ToString()); >You can get the GridBoundRecordState object for the given row using > >GridBoundRecordState rs = this.grid.Binder.GetRecordStateAtRowIndex(rowIndex); > > >The GridBoundRecordState has a LevelIndex property.


VL Vincent Lo April 20, 2004 06:22 AM UTC

ghlMkt is the parent GridHierarchyLevel, ghlMktSub is the child GridHierarchyLevel


VL Vincent Lo April 20, 2004 07:02 AM UTC

Thanks, I have solved it using the following code GridBoundRecordState rs = gridBinder.GetRecordStateAtRowIndex(e.RowIndex); CurrencyManager cm = (CurrencyManager) this.BindingContext[this.gridDataBoundGrid1.DataSource, this.gridDataBoundGrid1.DataMember]; DataRowView drv = (DataRowView) cm.Current; DataRow dr = drv.Row; if (rs.LevelIndex > 0) { dr = dr.GetChildRows(mDataSet.Relations[0])[rs.Position]; } GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell; int field = this.gridDataBoundGrid1.Binder.ColIndexToField(cc.ColIndex); string mappingName; if (rs.LevelIndex == 0) { mappingName = ghlMkt.GridBoundColumns[field].MappingName; } else { mappingName = ghlMktSub.GridBoundColumns[field].MappingName; } Console.WriteLine(mappingName+" "+ dr[mappingName].ToString()); } catch (Exception er) { Console.WriteLine(er.Message); } Could you advise if such code could be simplified a bit?


AD Administrator Syncfusion Team April 21, 2004 04:16 PM UTC

The GridBoundRecordState has a reference to the Table and the position of the record in this table. So, you might try code like: DataRowView drv = rs.Table[rs.Position] as DataRowView; to see if that gives you what you want. (I think the returned item is a DataRowView but you could check its type to see if something else is being returned in your case.)

Loader.
Up arrow icon