Link to home
Start Free TrialLog in
Avatar of hhnetworks
hhnetworksFlag for United States of America

asked on

C# asp.net web form Textbox string problem

I'm new to C# and ASP.NET so thanks in advance for help with this.

Im writing a simple web application that will display a DataGrid, and color the entire row based on the value of one column in the rows.
The column in my DB is called "Status" and the possible values will be "Requested", "In Transit" or "Received" with each value rendering a different color.

When debugging, it is showing my string value as "" even though there is data in the field.

My code is below:
using System;
using System.Data;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace NameOfMyApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (drv["Status"].ToString().Equals("Requested"))
                    { 
                        e.Row.BackColor = System.Drawing.Color.Tomato;
                    }
                else if (drv["Status"].ToString().Equals("In Transit"))
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                else
                    {
                        e.Row.BackColor = System.Drawing.Color.Lime;
                    }
            }
        }
    }
}

Open in new window


Its currently coloring all of my rows Green because of the empty value being returned.

Appreciate any help!
Avatar of plusone3055
plusone3055
Flag of United States of America image

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
               if (drv["Status"].ToString() == "Requested")
                    {
                        e.Row.BackColor = System.Drawing.Color.Tomato;
                    }
                else if (drv["Status"].ToString()  == "In Transit")
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }
                else
                    {
                        e.Row.BackColor = System.Drawing.Color.Lime;
                    }
if the value is showing ""  then you are not placing the correct value into your code
have you done a F10 Run to cursor to see if its catching that value ???
Avatar of hhnetworks

ASKER

Ive tried the code both ways (with the '==' and 'Equals') in the IF statement; and yes I have run to cursor and the string value is coming up empty, even though there's valid data in that column.

This was my reason for posting the question.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Edward
Edward
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Edward!!

You code worked perfectly!