Trying to access a specific GridView’s column is especially easy, but only if you know the column index. One way to handle accessing the column is to hard code the column index into the code:
GridView gv = new GridView();
DataControlField dcf = gv.Columns[5];
The main problem with the above solution is if you add a new column at index 1, you will need to manually change all hardcoded indexes. Therefore, another way to handle this is to set a constant integer for each column containing the index.
const int _MYCOLUMN = 5;
GridView gv = new GridView();
DataControlField dcf = gv.Columns[5];
Using this would only require you to change the _MYCOLUMN constant integer in one location, which would reflect in the rest of your code. However, an even better way to handle this is to access the column by a defined name. The method below involves setting the AccessibleHeaderText of a column on the aspx page when the gridview column is defined.
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”>
<Columns>
<asp:BoundField AccessibleHeaderText=”date” DataField=”date” HeaderText=”Date” SortExpression=”date” />
<asp:BoundField AccessibleHeaderText=”article” DataField=”article” HeaderText=”Article” SortExpression=”article” />
</Columns>
</asp:GridView>
By setting the AccessibleHeaderText, we can now access the column through this header name. The function below requires the gridview and a string containing the AccessibleHeaderText. The function will return the index of the column, if found . Otherwise, negative one (-1).
private int findColumnIndex(GridView gridView, string accessibleHeaderText)
{
for (int index = 0; index < gridView.Columns.Count; index++)
{
if (String.Compare(gridView.Columns[index].AccessibleHeaderText, accessibleHeaderText, true) == 0)
return index;
}
return -1;
}
By using this method, adding a new column in index one will not affect the rest of the code. The method checks the AccessibleHeaderText of each column to verify that it matches your specified string. If it matches, it returns the index.
int dateIndex = findColumnIndex(GridView1, “date”);
int authorIndex = findColumnIndex(GridView1, “author”);
int articleIndex = findColumnIndex(GridView1, “article”);
In the above example, dateIndex will return 0, authorIndex will return -1, and articleIndex will return 1. It’s as simple as that!
This entry was posted on Thursday, April 24th, 2008 at 7:53 am and is filed under C#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


good
July 20th, 2008 at 12:07 am
Ho\y He\\!
thenk you so much! I was just getting ready to write about 4,000 lines of code to show and hide coulms in asp.net vb intranet application ive been working on for a NFP when i found your post and i was able to write the whole routine in about 4 lines of code!!! based largly on what you have here. thank you so much!
-Popples
obvious but simple vb converson
Private Function GetGridViewColumnIndex(ByVal GridView As GridView, ByVal Column As String)
Dim Col As Integer
For Col = 0 To GridView.Columns.Count - 1
If Column = GridView.Columns.Item(Col).HeaderText Then
Return Col
Exit Function
End If
Next
Return -1
End Function
August 15th, 2008 at 7:27 pm