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
close icon

Get Column Index of GridGroupingControl when GroupingBy a different Column

I need to find the column index of a column in a GridGroupingControl WHEN it is grouped by a column other than the one I'm looking for.

I need to find it DURING: Grid_TableControlCurrentCellValidating()


I.e., my datasource has 4 columns:
A,B,C,D

If the user has grouped by B

I need to find the index of "D" if I do not know whether it is "D" or "d" (case)

The following works IF and ONLY IF there is NO grouping:

int colIndex = FindColumnIndex(grid.TableDescriptor.VisibleColumns, "d")


private int FindColumnIndex (GridVisibleColumnDescriptorCollection visibleColumns, string columnName)
{
int index = 1; //one based column indexes
columnName = columnName.ToLower();
foreach (GridVisibleColumnDescriptor col in visibleColumns)
{
if (col.Name.ToLower() == columnName)
{
return index;
}
index++;
}
return -1;
}

HOWEVER, whenever a user has grouped columns, the above code returns the wrong index.

For example:

grid.Model[grid.CurrentCell.RowIndex, colIndex].CellValue

This returns the value for the Column BEFORE the one I need, WHEN a field is grouped.

I have tried using the TableDescriptor.Columns collection instead of the VisibleColumns collection, but it's no better.

HELP!





5 Replies

AS Asarudheen S Syncfusion Team April 1, 2012 06:23 PM UTC

Hi Eric,

Thank you for your interest in Syncfusion products.

We are unable to reproduce the reported issue from our side. As a suggestion please make use of following code to resolve the issue.

int index= this.gridGroupingControl1.TableDescriptor.VisibleColumns.IndexOf(columname);

Could you please send us simple sample to replicate your issue so that it will more helpful to update you the exact solution?

Regards,
Asarudheen.



ER Eric Robishaw April 2, 2012 03:24 AM UTC

VisibleColumns.IndexOf(columname) only works if you have the exact case of the columnname...

But for various reasons, I do not always know the exact case of the column name, it could be Dog, DOG, or dog...





RB Ragamathulla B Syncfusion Team April 10, 2012 04:55 AM UTC

Hi Eric,

Thanks for the update.

You can get 'Column Index' of GridGroupingControl by using the following code.

//way 1
this.gridGroupingControl1.TableDescriptor.FieldToColIndex(fildname);
//way2 this.gridGroupingControl1.TableDescriptor.ColIndexToField(colindex);


Let me know if you have any further concerns.

Regards,
Ragamathullah B.



QU QLT User April 12, 2016 12:09 AM UTC

does this actually work ?
this.gridGroupingControl1.TableDescriptor.FieldToColIndex(fildname);

i think it takes an int and not a string. i would like to get the index from the columnname


PM Piruthiviraj Malaimelraj Syncfusion Team April 12, 2016 08:57 AM UTC

Hi QLT user,

Thank you for your interest in Syncfusion products.

The column collection is maintained in the TableDescriptor.Columns and TableDescriptor.VisibleColumns. In order to get the column index from column name , you can make use of IndexOf method of VisibleColumns or Columns. Please make use of below code and refer to the sample.

Code snippet

int index = this.gridGroupingControl1.TableDescriptor.VisibleColumns.IndexOf("ColumnName");

int id = this.gridGroupingControl1.TableDescriptor.Columns.IndexOf("ColumnName");



Sample

http://www.syncfusion.com/downloads/support/forum/103015/ze/DataGrid1575736822


Regards,

Piruthiviraj


Loader.
Live Chat Icon For mobile
Up arrow icon