How to know the Column number based on Column header in Grid View

Hi

I have a gridview with 3 columns. Say : id, Name and skill.

I want to know the column number of Column headers(May be Name,Skill or id)...

Thanks

Archan

0
Archan
5/22/2008 5:57:55 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
557 Views

Similar Articles

[PageSpeed] 1

I don't think there is any inbuilt way to do it. You have to loop through and compare the header text and get the index. 

private int GetColumnIndex (string headerText)
{
	int idx = 0;
	foreach (DataControlField gcf in GridView1.Columns)
	{
		if (gcf.HeaderText == headerText)
		{
			break;
		}
		idx++;
	}
	return idx;
}
 
Mark replies as answers if they helped you solve the problem.
0
siva_sm
5/22/2008 6:35:06 AM

Hi siva

Thanks for your quick reponse.

Archan

0
Archan
5/22/2008 7:48:27 AM

This is another way u can do it. Hope this helps u. This is my first post. 

 

private
int getColumnIndex(string text)

{

for(int cnt=0;cnt<GridView1.Columns.Count;cnt++)

{

if (GridView1.Columns[cnt].HeaderText == text)

{

return cnt;

break;

}

}

return -1;

}

0
aniil
5/22/2008 9:44:11 AM

Hi Anil

Thanks ...It is always helpful to know many ways to do the samething..

Archan

0
Archan
5/22/2008 9:47:23 AM
Reply: