0

Sorry for the mouthful of a title, but what I'm trying to do is really basic. In a inbox with messages, the DB for each message has a hasBeenRead bit. Once this message is clicked and "read", this bit is set to true, otherwise false.

I am wondering how I can essentially set every background color of those that have been read to something else than the default? I had some ideas, like going through every row and checking for that bit.......but assuming you have a big enough gridview with enough rows, wow that sounds very inefficient. I was also thinking I could store the color that the row should be in the database......but then I'm not sure how this would translate to actually changing the color of the row.

2

1 Answer 1

1

I had listView in my head when I wrote up my first answer.

But you can use the rowDataBound event and set the row's color in the code behind still.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
         DataKeyNames="xyz" onrowdatabound="GridView1_RowDataBound">
...
</asp:GridView>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{               
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         e.Row.BackColor = Color.FromName("#E56E94");     
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry I haven't had much time to come on and check this answer lately. So quick question. What is the DataKeyNames for? It doesn't seem to be used in the code behind? Also, I think what I would want to do is add the hasBeenRead flag into the column and hide it, and then do a if statement like if (e.Row.RowType.Cell[5] == false){change color;}? Would that work? I'm not sure what your if statement is doing/checking.
DataKeyNames is just the primary key of the data you're binding to the grid. So it might be "Id" or something similiar in your case. Looking at your code snippet, it seems good. You'd just have to stick that if inside the first one I have in the example above. Let me know if you need anything else :-)
So quick question. I tried to add in the code after your if statement if (e.Row.Cells[5].Equals("false")) { do something}. The problem is from what I can tell through debugging, that GridView1_RowDataBound is only hit when you load the page. After loading the page and click each row, that method is never hit again. Any suggestions would be greatly appreciated. Thanks!
Ahh you want to change the color when a row is clicked? You'll need to do something like this here also: stackoverflow.com/questions/7090086/…
Oh no sorry haha I am now confusing myself on what I want. I know how to change the color when the row is clicked I don't know what I was thinking in my last statement. So the gridview contains a list of private messages to the user. I want to change the color when it is loaded based on whether or not it has already been read, which is a column stored in the messages database. I figured if I put it as a hidden column into the gridview, I can check it someway and change the color based on the value?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.