gridview question for databinding

G

Guest

I am trying to set the text color of a cell in a data grid based on the a
value from the data source. I believe i should be doing it in
gridview_RowDataBound, but i do not know how to get to the value in the data
source. I'll try to explain:

column X should be red or green based on the value from szCellColor as
returned from the query used to fill the gridview. However, szCellColor is
NOT a column in the gridview.

Does anyone know how i can access this value from the query bound to the
gridview without the need to make it a hidden colmun?

Any hel pwould be appreciated
 
M

Mark Rae

I am trying to set the text color of a cell in a data grid based on the a
value from the data source. I believe i should be doing it in
gridview_RowDataBound, but i do not know how to get to the value in the
data
source. I'll try to explain:

column X should be red or green based on the value from szCellColor as
returned from the query used to fill the gridview. However, szCellColor is
NOT a column in the gridview.

Does anyone know how i can access this value from the query bound to the
gridview without the need to make it a hidden colmun?

Yup - all you need is to reference the e.Row.DataItem and cast it to a
DataRowView object e.g.

if ((((DataRowView)e.Row.DataItem)["szCellColor"]).ToString() == "red")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
}
else
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top