Problem modifying GridView cells at OnDataBound

G

gnewsgroup

I have a GridView which is bound to my database.

The leftmost column of the GridView has a value of either 0 or 1,
and I need to highlight the cell of column 3 if the leftmost column
has a value of 0. After highlighting, I want the leftmost column to
disappear.

So, in the OnDataBound event handler, I call a method HighlightCells()
which looks like this (code from my memory):

foreach (GridViewRow gvr in myGridView.Rows)
{
if (myGridView.Columns[0].Text.Equals("0"))
{
Label lbl = new Label();
lbl.Text = gvr.Cells[2].Text;
lbl.BackColor = Color.Yellow;
gvr.Cells[2].Text = String.Empty;
gvr.Cells[2].Controls.Add(lbl);
}

// Now let the leftmost column disappear!
myGridView.Columns[0].Visible = false;
}

The logic looks OK, right? It indeed works a little. myGridView
allows paging. Cells are not highlighted if I click page index to go
to another page. I did call HighlightCells() in the
OnPageIndexChanging event handler.

What is wrong? Or is there a better strategy to achieve my goal?
Thanks.
 
M

marss

I have a GridView which is bound to my database.

The leftmost column of the GridView has a value of either 0 or 1,
and I need to highlight the cell of column 3 if the leftmost column
has a value of 0. After highlighting, I want the leftmost column to
disappear.

Use OnRowDataBound event handler. It occurs when each single row is
bound. Also there is not need to add a label, you can paint a cell
itself.

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == "0")
{
e.Row.Cells[2].Style["background-color"] = "yellow";
}

e.Row.Cells[0].Visible = false;
}
}

Regards,
Mykola
http://marss.co.ua
 
G

gnewsgroup

I have a GridView which is bound to my database.
The leftmost column of the GridView has a value of either 0 or 1,
and I need to highlight the cell of column 3 if the leftmost column
has a value of 0. After highlighting, I want the leftmost column to
disappear.

Use OnRowDataBound event handler. It occurs when each single row is
bound. Also there is not need to add a label, you can paint a cell
itself.

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == "0")
{
e.Row.Cells[2].Style["background-color"] = "yellow";
}

e.Row.Cells[0].Visible = false;
}

}

Regards,
Mykolahttp://marss.co.ua

Thank you very much. I gave it a shot and it did work. It looks
perfect after I also hide the header of the leftmost column. I have
always been (and I am still) confused about OnDataBinding,
OnDataBound, OnRowDataBound events of a GridView.

BTW, does every visible control has a Style property?
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top