Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GridView rows.cells.GetCellIndex  based on ColumnName

Posted on 2007-03-20
4
Medium Priority
?
2,733 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 1500 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.

688 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