473,395 Members | 1,473 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,395 software developers and data experts.

GridView - RowCreated EventHandler doesn't work if page IsPostBack

Hello All,

I am trying to use the following RowCreated Event Handler to make the
BackColor of a cell different if it has a particular value.

protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

object cellValue = DataBinder.Eval(e.Row.DataItem,
"Primary Diagnosis");
if(cellValue != null)
{

if (cellValue.Equals("Unknown or Service Recipient
Declined"))
{
e.Row.Cells[1].BackColor = Color.Yellow;
}

}
}
}

The page works as expected the first time it is loaded. But if it is a
PostBack cellValue is null, so the code to color the cells BackColor
does not run. If I take out condition "if(cellValue != null) I get an
error message. How can I use the RowCreated Event handler to control
the BackColor of cells even when it is a PostBack.

Help is always appreciated. Thanks,

Paul

Apr 28 '07 #1
2 9025
Paul,

Two things:

1. Use RowDataBound event instead of RowCreated.

2. There is no need to call DataBinder.Eval. You can use e.Row.DataItem with
typecasting. If your datasource is a datatable, you can write something like

object cellValue = (DataRowView)e.Row.DataItem["Primary Diagnosis"];

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Paul" <pa************@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
Hello All,

I am trying to use the following RowCreated Event Handler to make the
BackColor of a cell different if it has a particular value.

protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

object cellValue = DataBinder.Eval(e.Row.DataItem,
"Primary Diagnosis");
if(cellValue != null)
{

if (cellValue.Equals("Unknown or Service Recipient
Declined"))
{
e.Row.Cells[1].BackColor = Color.Yellow;
}

}
}
}

The page works as expected the first time it is loaded. But if it is a
PostBack cellValue is null, so the code to color the cells BackColor
does not run. If I take out condition "if(cellValue != null) I get an
error message. How can I use the RowCreated Event handler to control
the BackColor of cells even when it is a PostBack.

Help is always appreciated. Thanks,

Paul

Apr 29 '07 #2
Eliyahu,

Works beautifully. Thanks,

Paul

Apr 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Bugden | last post by:
Hi, I would like to set the column widths in the ASP.NET GridView control at runtime I have tried the following: http://msdn2.microsoft.com/en-us/library/ms178296.aspx Which uses the...
10
by: NH | last post by:
I have a girdview with paging enabled. How can I add a message in the footer to say "Viewing records 1-15 of 45" etc Thanks
0
by: John Smith | last post by:
ASP.Net 2.0 / C# / IIS 6 I have 2 pages. The master page consists of a tabbed menu created using the Menu and MultiView controls. Something like this: ...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
5
by: maurban | last post by:
Hi there experts, I have a gridview with a couple textboxes and a dropdownlist. I'm trying to insert a default value into my database driven dropdownlist. I'm doing this in the rowdatabound...
6
by: AG | last post by:
I have a gridview with a template column containing an imagebutton to delete the row. Under some condition I don't want the row to be deleted, so would like to remove the button. In the...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
0
by: F | last post by:
Hi, The funniest part of the problem described here is that I am not new to ASP.NET, but honestly here I have no idea about what's going on. I have a GridView with a data source that just...
4
by: jack | last post by:
Hi, Consider the following handler: protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType != DataControlRowType.DataRow)...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.