Solved

GridView rows.cells.GetCellIndex  based on ColumnName

Posted on 2007-03-20
4
2,686 Views
Last Modified: 2013-11-07
I have a gridview which I have bound to a ObjectDataSource.  One of the fields in my DataSource is "Balance".  I am using the GridView.RowCreated event to format the background color of the "Balance" Cell the amount is negative. How can I retrieve the the CellIndex of the the "Balance" cell?  

Or would I retrieve the Column Index of the "Balance" field?

Thanks Dan

0
Comment
Question by:DigitalDan3
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
G0ggy earned 500 total points
ID: 18757408
0
 

Author Comment

by:DigitalDan3
ID: 18757712
The HeaderRow property is available only after the GridView control creates the header row in the RowCreated event.

I am using the GridView.RowCreated event to style the individual cell. Seeing how the HeadRow is already created at this point I can use the header row.

If I can retrieve the TableCell that for the datafield "Balance"  can then determine the index.

What I need is syntax simualar to
dim x as integer = me.gridview1.headerrow.cells.getCellIndex("Balance")

0
 

Author Comment

by:DigitalDan3
ID: 18759351
Here's a code sample I am using

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then

            'Get the title of courtesy for the item that's being created.
            Dim MaritalStatus As String = CStr(DataBinder.Eval(e.Row.DataItem, "MaritalStatus"))
            If MaritalStatus = "M" Then
                e.Row.BackColor = Drawing.Color.Red
            Else
                e.Row.BackColor = Drawing.Color.Yellow
            End If
        End If
    End Sub

Instead of changing the row's back color I would like to change the maritalStatus cell's color.
0
 

Expert Comment

by:YakovS
ID: 25327990
Below is a handy function to get cell index by HeaderText
 protected int ColumnID(string HeaderText)
    {
        foreach (DataControlFieldHeaderCell cell in GridView1.HeaderRow.Cells)
        {
            if (cell.ContainingField.HeaderText == HeaderText)
                    return GridView1.HeaderRow.Cells.GetCellIndex(cell);
        }
 
        return -1;
    }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

828 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