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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Abhishek KumarRank #510 · 4.8K reputationPosted Oct 10, 2014, 2:10 AM EDT
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
Kishan Tanpure97 reputationPosted Oct 10, 2014, 3:48 AM EDT
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 KumarRank #510 · 4.8K reputationPosted Oct 11, 2014, 12:16 AM EDT
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
Abhishek KumarRank #510 · 4.8K reputationPosted Oct 11, 2014, 12:17 AM EDT
Abhishek
Kishan Tanpure97 reputationPosted Oct 12, 2014, 9:02 PM EDT
Netaji Chavan171 reputationPosted Apr 9, 2015, 1:21 AM EDT