Right, almost got it although it's needs some work, and unfortunately I use c# and you use VB, but barring that...
I created a function that will return the ColumnIndex of the column that the Grid is being sorted by, if the Grid display order of columns, is the same as the DataTabel order of columns.
This takes an input of the SortBy expression, (string ColumnName), the DataGrids DataSource property cast as a DataSet, (DataSet ds), and the DataGrids DataMember, (string table). This would be done after filling the DataSet and binding the DataGrid kin of like this...
<CODE>
GetColumnIndex(SortBy, (DataSet) dg.DataSource,dg.DataMember)
</CODE>
The function itself uses the passed DataSet (ds) and string (table) variables to get a pointer to the DataGrid's source table as a DataTable. Using an Int return variable defaulted to zero at the start of the function, we then iterate through the columns collection of the DataTable looking for a match between the ColumnName of the DataTable.Column, and the SortBy expression. We increment our return variable every time we find a Column doesn't match, and when it does we break out if the iteration, which gives us the Index of the Column in the DataTable, (I have tried this, and it does work...), which is returned to the function making the call.
<CODE>
public int GetColumnIndex(string ColumnName, DataSet ds, string table)
{
int Index = 0;
DataTable t = new DataTable();
t = ds.Tables
;
foreach (DataColumn c in t.Columns)
{
if (c.ColumnName == ColumnName)
{
break;
}
else
{
Index += 1;
}
}
return Index;
}
</CODE>
Of course, if we have ButtonColumns preceding our DataColumns in the DataGrid, we either need to get a count of those, or hard code the number of them, and add it to the returned index to get the Column Index of the Sort By column in the DataGrid.
By passing the ColumnIndex, (returned by the above function), DataGrid and SortBy expresssion to the below function, we can add the number of buttonColumns preceding our sorted by column to the ColumnIndex value.
<CODE>
public int AddButtonColumnsToIndex(int ColumnIndex, DataGrid dg, string SortBy)
{
foreach (DataGridColumn c in dg.Columns)
{
if (c.SortExpression != SortBy)
{
if (c is System.Web.UI.WebControls.ButtonColumn)
{
ColumnIndex += 1;
}
else
{
ColumnIndex += 0;
}
}
else
{
break;
}
}
return ColumnIndex;
}
</CODE>
This works by iterating through our passed DataGrids column collection, and validating each columns SortExpression property against the SortExpression the Grids being sorted by. Until we get a match between these, we check the WebControl type of each column, and if it's a button column, we add one to our ColumnIndex. This should add the total number of button columns preceding our sort by column to our Index value, so the value this returns should be the correct DataGrid column index of the column the datagrid is being sorted by. And thus we have a pointer to the column of the datagrid wehich it is being sorted by...
If you need any clarity in this, (and from re-reading it you might!), just post back again...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.