Solved

GridView - Get cell by name?

Posted on 2006-07-21
3
1,667 Views
Last Modified: 2008-01-09
In my GridView I have a column, example:
<asp:hyperlinkfield headertext="Cheese" />

I've got a handler on the RowDataBound event.

Currently to access that cell I use:
e.Row.Cells(6)

I'm convinced there is some way using the framework to access that cell by something other than the Integer index, which is very messy.

I'd much rather iterate through somehow, find the cell based on the "headertext" property = "Cheese"

Can it be done?
0
Comment
Question by:renesisx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 500 total points
ID: 17157368
You can use the following method to retrieve the column index of a GridView column by name:

     int GetColumnIndex(GridView grid, string colName)
    {
        int intCount = 0;
        foreach (DataColumn col in grid.Columns)
        {
            if (col.ColumnName.ToLower().Trim() == colName.ToLower().Trim())
            {
                return intCount;
            }
            intCount++;
        }
        return -1;
    }
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17157374
E.g.:
    e.Row.Cells(GetColumnIndex("Cheese"))
0
 
LVL 1

Author Comment

by:renesisx
ID: 17157490
That did the trick! Nice one :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall‚Ķ
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question