I am using a ASP.Net Datagrid in my Project.
I want to build a function that returns me Column Index when I provide Column Name to it.
Please, help Me.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Netaji ChavanPosted Apr 9, 2015, 5:21 AM
Kishan TanpurePosted Oct 13, 2014, 1:02 AM
Abhishek KumarPosted Oct 11, 2014, 4:17 AM
Abhishek
Abhishek KumarPosted Oct 11, 2014, 4:16 AM
Private Function GetColumnIndex(grid As GridView, ColName As String) As Integer
For Each col As DataControlField In grid.Columns
If col.HeaderText.ToLower().Trim() = ColName.ToLower().Trim() Then
Return grid.Columns.IndexOf(col)
End If
Next
Return -1
End Function
Abhishek
Kishan TanpurePosted Oct 10, 2014, 7:48 AM
I converted the code given by you into VB.Net as I am using VB.Net for my coding
purpose.
The code which I wrote based on your code is as below.
But it is giving me an Error on Line 5;
Error is :
Value of type 'System.Web.UI.WebControls.DataControlField' cannot be converted to 'System.Web.UI.WebControls.DataGridColumn'
Abhishek KumarPosted Oct 10, 2014, 6:10 AM
Please try to use the below function.
private int GetColumnIndex(GridView grid, string ColName)
{
foreach (DataControlField col in grid.Columns)
{
if (col.HeaderText.ToLower().Trim() == ColName.ToLower().Trim())
{
return grid.Columns.IndexOf(col);
}
}
return -1;
}
Hope this will help.
Abhishek