Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

How to find the Column Index By Name in an ASP.Net DataGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DBLWizard - 18 Jul 2005 22:05 GMT
Howdy All,

I am wondering if there is a way to find the column index by the header
name.  What I am trying to get away from is using a hardcoded number in
the ItemDataBound event.  Something like this:

private void grdTestScores_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    DataRowView drvData = (DataRowView)e.Item.DataItem;
    Button btnDelete;

    if ((e.Item.ItemType == ListItemType.Item) ||
           (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        btnDelete = (Button)e.Item.Cells[16].Controls[0];

        if (btnDelete.CommandName == "Delete")
        {
            btnDelete.Attributes.Ad("onclick", "return ConfirmDelete();");
        }

    }
...

I don't want to hardcode the column number 16 in the above example.

Thanks in advance.

dbl
Eliyahu Goldin - 19 Jul 2005 09:39 GMT
If the columns are not auto-generated, you can use datagrid Columns
collection. Write a utility function that will take a column name as a
parameter, loop through Columns collection, find a column with matching
name, get it's index and return Cells element with the same index.

Eliyahu

> Howdy All,
>
[quoted text clipped - 26 lines]
>
> dbl

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.