Solved

GridView rows.cells.GetCellIndex  based on ColumnName

Posted on 2007-03-20
4
2,655 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 new code templates in NetBeans IDE 8.0 for Windows.

943 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

7 Experts available now in Live!

Get 1:1 Help Now