Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional formatting, gridview and invisible field

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
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

 
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.
 
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?
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top