473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 15701
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen | last post by:
My primary GridView is firing an OnDataBound after Selecting a row (which triggers SelectedIndexChanged). This only happens in the following scenario: - User enters a textbox search value and...
0
by: Fernando Monteiro | last post by:
Hello, Could anyone please help me ? I've a GridView and I'd like to insert one of the girdview's dataset field as a row tooltip. The grid view is declared like: <asp:GridView...
1
by: tfsmag | last post by:
Hello, I have a function that returns a dynamically created gridview. This works fine, however it does not seem to be able to maintain state when adding sorting or paging to the gridview. Does...
0
by: den 2005 | last post by:
Hi everybody, I created a Gridview with a TemplateField and there is Label control in ItemTemplate and a DropdownList control in EditItemTemplate, I was to displayed them ok when I click the...
2
by: David C | last post by:
I have a need to make a GridView row a different color (red) if a date exists on the row. I assume I would do this in the OnDataBound event. I know how to make a Cell(x) a different color but not...
0
by: pargat.singh | last post by:
Hi: I am using a grid and having a problem with sorting. Below are my code and change the header name at run time as below based on language.If i don't change the header name it work. Can...
0
by: pargat.singh | last post by:
Hi: I am using a grid and having a problem with sorting. Below are my code and change the header name at run time as below based on language.If i don't change the header name it work. Can...
0
by: Stan SR | last post by:
Hi, I need to customize a pargertemplate with these items a arrow to go back to the 1rst page a arrow for the previous page a dropdownlist with the page indexes a arrow for the new page a...
0
by: edurazee | last post by:
I am able to create BoundFields and Footer-rows dynamically like this in my GridView: protected void Page_Load(object sender, EventArgs e) { CreateGridView(); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.