Solved

GridView rows.cells.GetCellIndex  based on ColumnName

Posted on 2007-03-20
4
2,635 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
Comment Utility
0
 

Author Comment

by:DigitalDan3
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

743 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

15 Experts available now in Live!

Get 1:1 Help Now