Solved

GridView - Get cell by name?

Posted on 2006-07-21
3
1,658 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
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 500 total points
Comment Utility
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
Comment Utility
E.g.:
    e.Row.Cells(GetColumnIndex("Cheese"))
0
 
LVL 1

Author Comment

by:renesisx
Comment Utility
That did the trick! Nice one :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now