469,160 Members | 2,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

Problem modifying GridView cells at OnDataBound

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.
Nov 21 '07 #1
3 15544
On 21 μΙΣ, 05:47, gnewsgroup <gnewsgr...@gmail.comwrote:
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

Nov 21 '07 #2
On Nov 21, 1:03 am, marss <marss...@gmail.comwrote:
On 21 μΙΣ, 05:47, gnewsgroup <gnewsgr...@gmail.comwrote:
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?
Nov 21 '07 #3
On 21 μΙΣ, 15:54, gnewsgroup <gnewsgr...@gmail.comwrote:
BTW, does every visible control has a Style property?
Controls inherited from HTMLControl or WebControl have Style property.
Nov 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Fernando Monteiro | last post: by
1 post views Thread by tfsmag | last post: by
2 posts views Thread by David C | last post: by
reply views Thread by pargat.singh | last post: by
reply views Thread by pargat.singh | last post: by
reply views Thread by Stan SR | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.