INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Conditional formatting, gridview and invisible field

Conditional formatting, gridview and invisible field

(OP)
OK, I have a page with conditional formatting that works when I use this code

CODE

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Status
                string ncrstatus = Convert.ToString(e.Row.Cells[5].Text);
                e.Row.Cells[0].BackColor = System.Drawing.Color.FromName(ncrstatus); 
            } 

But when I use this code (with DataKeyNames defined in the gridview definition)

CODE

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Status
                string ncrstatus = gvNCR.DataKeys[0].Values["ncrstatus"].ToString();
                e.Row.Cells[0].BackColor = System.Drawing.Color.FromName(ncrstatus); 
            } 

It shows everything as the first ncrstatus that it encounters in the entire recordset. I would liek to use the second one because I do not want to show the ncrstatus field, I just want to use it for my conditional formatting, but I can only set visible to false if I use the datakeys. I am sure that I am missing something simple. I have tried a few different iterations, my problem keeps coming back to it seems to be not stepping thru the array and I do not know C# well enough yet.

Any help?
Thanks,
Willie

RE: Conditional formatting, gridview and invisible field

Sorry your question is confusing. What exactly are you trying to do?
In the first you are getting a value from the cell (which I suggest you don't do)
In the second you are going after a value based on a DataKey.

Need some help understanding your end goal.

RE: Conditional formatting, gridview and invisible field

(OP)
I want to get the value from the DataKey, which I try to do in the second one and it works. Sort of. When I use the DataKey it only returns the first value in the array and uses that for every row. Do I have to iterate thru the array manually, or is there something built into C# that will do that for me?

RE: Conditional formatting, gridview and invisible field

CODE

string ncrstatus = gvNCR.DataKeys[0].Values["ncrstatus"].ToString(); 
Gets the value fromthe DataKeys collection, so yes, the value will be the same for every row. If you want the value of something for each row, you need to use the value you got from the collection. However, you don't get the value from the grid itself, you should be getting the value from the data being bound to the grid.

Try something like:

CODE

string ncrstatus = gvNCR.DataKeys[e.RowIndex].Value 

RE: Conditional formatting, gridview and invisible field

(OP)
Thanks, I tried that and couldn't come up with the proper syntax. I hadn't tried your suggestion, but that doesn't get it either. However, a little modification

CODE

string ncrstatus = gvNCR.DataKeys[e.Row.RowIndex].Value.ToString(); 

gave me exactly what I need.

thanks,
wb

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close