Solved

Convert Gridview VB to C#

Posted on 2010-11-16
25
387 Views
Last Modified: 2012-05-10
Can anyone help me convert the below from vb to c#




Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

        If DataBinder.Eval(e.Row.DataItem, "alert") = 1 Then
            CType(e.Row.FindControl("Label2"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label2"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label3"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label3"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label4"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label4"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label5"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label5"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label6"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label6"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label7"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label7"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label8"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label8"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label9"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label9"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label11"), Label).BackColor = Drawing.Color.Red
            CType(e.Row.FindControl("Label11"), Label).ForeColor = Drawing.Color.White
        End If

        If DataBinder.Eval(e.Row.DataItem, "alert") = 2 Then
            CType(e.Row.FindControl("Label2"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label2"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label3"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label3"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label4"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label4"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label5"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label5"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label6"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label6"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label7"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label7"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label8"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label8"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label9"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label9"), Label).ForeColor = Drawing.Color.White
            CType(e.Row.FindControl("Label11"), Label).BackColor = Drawing.Color.Orange
            CType(e.Row.FindControl("Label11"), Label).ForeColor = Drawing.Color.White
        End If


    End Sub
0
Comment
Question by:LBGUC04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 5
  • +1
25 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34147465
You can use the online conversion free service at
http://www.developerfusion.com/tools/convert/vb-to-csharp/
 
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
	if (DataBinder.Eval(e.Row.DataItem, "alert") == 1) {
		((Label)e.Row.FindControl("Label2")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label2")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label3")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label3")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label4")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label4")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label5")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label5")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label6")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label6")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label7")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label7")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label8")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label8")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label9")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label9")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label11")).BackColor = System.Drawing.Color.Red;
		((Label)e.Row.FindControl("Label11")).ForeColor = System.Drawing.Color.White;
	}

	if (DataBinder.Eval(e.Row.DataItem, "alert") == 2) {
		((Label)e.Row.FindControl("Label2")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label2")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label3")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label3")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label4")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label4")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label5")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label5")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label6")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label6")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label7")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label7")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label8")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label8")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label9")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label9")).ForeColor = System.Drawing.Color.White;
		((Label)e.Row.FindControl("Label11")).BackColor = System.Drawing.Color.Orange;
		((Label)e.Row.FindControl("Label11")).ForeColor = System.Drawing.Color.White;
	}


}

Open in new window


Regards
Raj
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34147475
One more conversion service from Telerik
http://converter.telerik.com/

Raj
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34147517
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)

        if (DataBinder.Eval(e.Row.DataItem, "alert") == 1)
        {
            (e.Row.FindControl("Label2") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label2") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label3") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label3") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label4") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label4") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label5") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label5") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label6") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label6") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label7") as  Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label7") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label8") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label8") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label9") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label9") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label11") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label11") as Label).ForeColor = Drawing.Color.White;
        }

        if (DataBinder.Eval(e.Row.DataItem, "alert") == 2)
        {
            (e.Row.FindControl("Label2") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label2") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label3") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label3") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label4") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label4") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label5") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label5") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label6") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label6") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label7") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label7") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label8") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label8") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label9") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label9") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label11") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label11") as Label).ForeColor = Drawing.Color.White;
        }
}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Expert Comment

by:chillyfrost
ID: 34147716
protected void  // ERROR: Handles clauses are not supported in C#
GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
      if (DataBinder.Eval(e.Row.DataItem, "alert") == 1) {
            ((Label)e.Row.FindControl("Label2")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label2")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label3")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label3")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label4")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label4")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label5")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label5")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label6")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label6")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label7")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label7")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label8")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label8")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label9")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label9")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label11")).BackColor = Drawing.Color.Red;
            ((Label)e.Row.FindControl("Label11")).ForeColor = Drawing.Color.White;
      }

      if (DataBinder.Eval(e.Row.DataItem, "alert") == 2) {
            ((Label)e.Row.FindControl("Label2")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label2")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label3")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label3")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label4")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label4")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label5")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label5")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label6")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label6")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label7")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label7")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label8")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label8")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label9")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label9")).ForeColor = Drawing.Color.White;
            ((Label)e.Row.FindControl("Label11")).BackColor = Drawing.Color.Orange;
            ((Label)e.Row.FindControl("Label11")).ForeColor = Drawing.Color.White;
      }


}
0
 

Author Comment

by:LBGUC04
ID: 34153125

RajkumarGS:

I get the error

Error      1      Type or namespace definition, or end-of-file expected





carl_tawn:

I got a whole load of invalid token errors




chillyfrost:
I got this error

Error      1      Type or namespace definition, or end-of-file expected


0
 

Author Comment

by:LBGUC04
ID: 34153129
RajkumarGS: & chillyfrost:
I fixed the issue with the {} this is the error i get

Error      1      Operator '==' cannot be applied to operands of type 'object' and 'int'
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153135
I missed the opening brace. Should have been:

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
        if (DataBinder.Eval(e.Row.DataItem, "alert") == 1)
        {
            (e.Row.FindControl("Label2") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label2") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label3") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label3") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label4") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label4") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label5") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label5") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label6") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label6") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label7") as  Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label7") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label8") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label8") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label9") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label9") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label11") as Label).BackColor = Drawing.Color.Red;
            (e.Row.FindControl("Label11") as Label).ForeColor = Drawing.Color.White;
        }

        if (DataBinder.Eval(e.Row.DataItem, "alert") == 2)
        {
            (e.Row.FindControl("Label2") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label2") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label3") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label3") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label4") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label4") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label5") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label5") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label6") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label6") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label7") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label7") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label8") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label8") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label9") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label9") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label11") as Label).BackColor = Drawing.Color.Orange;
            (e.Row.FindControl("Label11") as Label).ForeColor = Drawing.Color.White;
        }
} 

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34153138
>> Type or namespace definition, or end-of-file expected

This error will show when you placed the piece of code in wrong place.
For eg:- If you place this code inside page_load, it will show the same error. Because this is an individual procedure

Verify this

Raj
0
 

Author Comment

by:LBGUC04
ID: 34153151
ok well for all of the examples i get the following and the DataBinder is underlines with a blue line

Error      1      Operator '==' cannot be applied to operands of type 'object' and 'int'
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34153152
My code actually have the opening & closing brace properly. Are you sure you put it in correct place (not into another procedure /event)

Raj
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153157
You're either going to need to parse the value, or compare to a string:

if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "alert").ToString()) == 1)
{
    // etc, etc
}

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34153161
You need to cast

For eg:- Change
if ( DataBinder.Eval(e.Row.DataItem, "alert") == 1)
to
 if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "alert")) == 1)

Raj
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153170
Of course, you could just cut your code in half and use:

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            int alert = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "alert").ToString());
            Color backColor = (alert == 1 ? Drawing.Color.Red : Drawing.Color.Orange);

            (e.Row.FindControl("Label2") as Label).BackColor = backColor;
            (e.Row.FindControl("Label2") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label3") as Label).BackColor = backColor;
            (e.Row.FindControl("Label3") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label4") as Label).BackColor = backColor;
            (e.Row.FindControl("Label4") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label5") as Label).BackColor = backColor;
            (e.Row.FindControl("Label5") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label6") as Label).BackColor = backColor;
            (e.Row.FindControl("Label6") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label7") as Label).BackColor = backColor;
            (e.Row.FindControl("Label7") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label8") as Label).BackColor = backColor;
            (e.Row.FindControl("Label8") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label9") as Label).BackColor = backColor;
            (e.Row.FindControl("Label9") as Label).ForeColor = Drawing.Color.White;
            (e.Row.FindControl("Label11") as Label).BackColor = backColor;
            (e.Row.FindControl("Label11") as Label).ForeColor = Drawing.Color.White;
            }
        }

Open in new window

0
 

Author Comment

by:LBGUC04
ID: 34153316
The page now runs but it isnt showing the highlighted rows. I placed a debug catch on the code but it didnt pause on this code at all?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153415
That means the event handler isn't hooked up properly. Switch to design view, select your grid then, in the properties window click the lightning bolt (which is properties). Scroll down till you find RowCreated and then set its value to "GridView1_RowCreated".
0
 

Author Comment

by:LBGUC04
ID: 34153447
Thanks i have got that hooked up now

I am now seeing an eror on the convert

Line 49:     protected void GridMain_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
Line 50:     {
Line 51:         if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "alert").ToString()) == 1)
Line 52:         {
Line 53:             (e.Row.FindControl("Label2") as Label).BackColor = System.Drawing.Color.Red;
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153453
What error?
0
 

Author Comment

by:LBGUC04
ID: 34153455
Sorry i didnt copy it all


Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 49:     protected void GridMain_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
Line 50:     {
Line 51:         if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "alert").ToString()) == 1)
Line 52:         {
Line 53:             (e.Row.FindControl("Label2") as Label).BackColor = System.Drawing.Color.Red;
 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153473
OK, that means that either DataItem is null, or it can't find the label.

Odds are it is because it is also firing for the header row, which will not have an associated DataItem. Try wrapping it with an additional "if":

protected void GridMain_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "alert").ToString()) == 1)
        {  
             (e.Row.FindControl("Label2") as Label).BackColor = System.Drawing.Color.Red;
             ....
        }
    }
}

Open in new window

0
 

Author Comment

by:LBGUC04
ID: 34153526
That did work tank you for your assistance it getting it working.

I dont have another question though. I am refreshing my gridview every 5 seconds using the script manager and update panel. Since putting in the row update code i get an error when the timer triggers

A runtime error has occured

sys.webforms.pagerequestmanagerservererrorexception: object reference not set to an instance of an object
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34153537
You'll need to put a breakpoint on the methods that draw your grid and see what is happening. ScriptManager messages are usually a bit vague.
0
 

Author Comment

by:LBGUC04
ID: 34153673
It look slike its happening because on timer event we get a e.Row.DataItem is null is there a way to almost say


coalesce(e.Row.DataItem,0)

or

isnull(e.Row.DataItem,0)
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34153709
You can simply check if it is null before attempting to access it:

if (e.Row.DataItem != null)
{
     // do stuff
}

Open in new window

0
 

Author Comment

by:LBGUC04
ID: 34153722
Thats great its working now thank you for your assistance
0
 

Author Closing Comment

by:LBGUC04
ID: 34153728
Very Helpful
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question