We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How do I get the column number when the columns are reordered

Hi, I have a databound grid. Its datasource is a DataTable. It contains 4 columns ID, Comment, Date and Priority. Say, I clicked on Comment cell on first row. I used: int col = grid.Binder.ColIndexToField(grid.CurrentCell.ColIndex), it returned 2. dataTable.Columns[col].ColumnName returns "Comment". It is good so far. Now I moved Comment field to the end (4). The above code returns col 4 and the column name is "Priority". I am interested in getting my DataColumn from DataTable for my current cell after reordering. How do I get that? Also, how do I get the current row after sorting the column (double clicking on a col header)? thanks, - Reddy

2 Replies

CK Curtis Koppang April 2, 2003 03:20 PM UTC

I think I have seen the answer to both of your questions in previous postings, you may poke around a little to find them.


AD Administrator Syncfusion Team April 2, 2003 03:41 PM UTC

The simplest thing to do is to get the field from the the column index, an dthen use teh field in teh GridBoundColumns (or Binder.InternalColumns) to get the assocuiated MappingName which should be the columnname.
GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
int field = this.gridDataBoundGrid1.Binder.ColIndexToField(cc.ColIndex);
string mappingName = this.gridDataBoundGrid1.Binder.InternalColumns[field].MappingName;
Once you have teh columnname, you can use that to index the columns in your DataTable. One way to get the current row in the DataTable is to use the CurrencyManager.Current property.
CurrencyManager cm = (CurrencyManager)this.BindingContext[this.gridDataBoundGrid1.DataSource, this.gridDataBoundGrid1.DataMember];
DataRowView drv = (DataRowView)cm.Current;
DataRow dr = drv.Row;

Loader.
Up arrow icon