Solved

GridView rows.cells.GetCellIndex  based on ColumnName

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

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

Suggested Solutions

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:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

751 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