How can I, given a column name or index, determine where it appears in the grid? (e.g. If my datatable columns are name, address, city, state, zip, I want to know that zip is the fifth column, unless of course I, or the user, moved it).
See GridDataBoundGrid.Binder.NameToField and NameToColIndex.
IPIvan PellyNovember 25, 2002 12:26 PM UTC
Is NameToColIndex going to return the physical order of the column, or just a numeric index to the column?
AdministratorNovember 25, 2002 01:42 PM UTC
I am not sure I understand the difference between "physical order of the column" and "numeric index to the column". By physical order, do you mean the order of the column in the datatable and not its order in the grid?
NameToField looks for the column in the GridBoundColumns (or InternalColumns) which has the same MappingName as the passed value. It returns a value based on the index of this column in the GridBoundColumns (or InternalColumns). This reflects the position of the column in the GridDataBoundGrid. So, if a 1 is returned, the column is the left-most column of the grid, and 2 would be the next column, and so.
So, if the column whose mappingname is Company is moved from gridcolumn 1 to gridcolumn 5, then NameToField("Company") would change from returning 1 to returning 5.
IPIvan PellyDecember 2, 2002 06:35 PM UTC
I was operating under the assumption that a column's index would be an immutable handle to that column, regardless of its physical position within the grid - apparantly this is not so.
Would it be true then that the only reliable way to get a reference to a column, regardless of its original or current position, is via the column name?
It seems that GridDataBoundGrid1.GridBoundColumns is a zero-based collection, but if you take the name you get from GridDataBoundGrid1.GridBoundColumns.Item(X).MappingName, and feed it into GridDataBoundGrid1.Model.NameToColIndex(s), you don't get back X, but X + 1, so NameToColIndex is one-based?
Either way, it looks like both GridDataBoundGrid1.GridBoundColumns.Item() and NameToColIndex return numbers that are related to the physical location of the column, albeit if one of them is off by one...
AdministratorDecember 2, 2002 10:11 PM UTC
There are two collections of 'columns' in a GridDataBoundGrid.
One is the GridDataBoundGrid.GridBoundColumns (or an equivalent collection, GridDataBoundGrid.Binder.InternalColumns). This collection is indexed zero-based as it is an array of columns bound to the datasource.
The other 'columns' collection is the GridDataBoundGrid.Model.Cols collection (and other Model collections). This collection is 'essentially' one-based as column zero in this collection is the row header column.
Since the row header column is not reflected in the first column collection (GridBoundColumns), but is relected in the second collection, this is why the first collection is zero based and the second collection is one-based.
If you physically moved the columns, then both these collections will be changed to reflect this move. So, you cannot rely on the index of a column in either of these collections to be the same as the index into the unmoved datasource (DataTable). So, if you need to get at a particular column in the underlying DataTable, you would want to use the name as the index.