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

Converting primary key & Column Name to grid row & column indices

I''m recording the ID of a record and the name of the column. I need to retrieve the corresponding row & column indexes for the represented grid cell. The code (shown below) works for grids with one row per record, but I can''t seem to find the information needed to adjust the indices when there''s two rows per record. FindRowID() ----------- tring primaryKeyColumnName = ((DataTable)(this.Binder.DataSource)).PrimaryKey[0].ColumnName; int gridRowIndex = 0; CurrencyManager cmGrid = (CurrencyManager)this.BindingContext[this.DataSource,this.DataMember]; for (int i = 0; i < this.Binder.RecordCount; i++) { DataRowView drvGridDataRow = (DataRowView) cmGrid.List[i]; if ((Guid)drvGridDataRow[primaryKeyColumnName] == guidPrimaryKeyValue) { gridRowIndex = (i*(this.Model.Rows.HeaderCount+1)) + this.FirstDataRow; break; } } ------------------------------------------------ Note: FirstDataRow is contains # of data rows/rec FindColumnNum() --------------- int intColumnNum = this.NameToColIndex(strColumnName); ------------------------------------------------

1 Reply

AD Administrator Syncfusion Team April 8, 2005 06:41 PM

If you know the column name, colName, and the position in the CurrencyManager, recPosition, then try this code:
int field = this.gridDataBoundGrid1.Binder.NameToField(colName);
int rowIndex = this.gridDataBoundGrid1.Binder.ListManagerPositionToRowIndex(recPosition);
int rowOffSet;
int colIndex = this.gridDataBoundGrid1.Binder.GetHierarchyLevel(0).FieldToRowField(field, out rowOffSet);//.NameToColIndex("City"));
rowIndex += rowOffSet;
if(this.gridDataBoundGrid1.Binder.GetHierarchyLevel(0).RowCountPerRecord == 0)
	colIndex += 1;
Console.WriteLine("col={0} row={1}", colIndex, rowIndex );

Live Chat Icon For mobile
Up arrow icon