locked
Change GridView row color based on condition in DataBinder Eval RRS feed

  • Question

  • User1872364149 posted

    Hi,

    I want to change a particular row color of GridView based on some condition.

    I am using ASP.NET with c#.

    I have tried without success this code-behind:

    if (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, 
       "myHour")).ToString("HH:mm:ss") >= "00:00:00")
    {
        e.Row.BackColor = Color.Red;
    }

    I have error:

    Operator '>=' cannot be applied to operands of type 'string' and 'string'

    Can you help me?

    Thank you in advance for any help, really appreciated.

     

    Wednesday, October 4, 2017 1:06 PM

Answers

  • User-1838255255 posted

    Hi cms9651,

    According to your description and needs, you want to compare datetime, then change the gridview row backgrond color, i make a sample, please check:

    Sample Code:

    <asp:GridView ID="GridView2" runat="server"
                    EnableTheming="True" AutoGenerateColumns="false" OnRowDataBound="GridView2_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Item" HeaderText="Item" />
                        <asp:BoundField DataField="Rmk_Dt" HeaderText="Rmk_Dt" />
                    </Columns>
    </asp:GridView>
    
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[2] {
                new DataColumn("Item", System.Type.GetType("System.String")),
                new DataColumn("Rmk_Dt", System.Type.GetType("System.DateTime"))
            });
                    dt.Rows.Add("Shirt", DateTime.Parse("9/3/2017 20:00:00"));
                    dt.Rows.Add("Jeans", DateTime.Parse("10/15/2017 20:00:00 "));
                    dt.Rows.Add("Trousers1", DateTime.Parse("9/29/2017 20:00:00"));
                    dt.Rows.Add("Tie", DateTime.Parse("10/5/2017 23:30:00"));
                    dt.Rows.Add("Trousers2", DateTime.Parse("10/10/2017 20:00:00"));
                    dt.Rows.Add("Cap", DateTime.Parse("10/4/2017 20:00:00"));
                    dt.Rows.Add("Cap2", DateTime.Parse("10/15/2017 20:00:00"));
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }
            }
            string comdate = "10/05/2017 23:00:27";
            protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DateTime datetime__1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Rmk_Dt").ToString());
                    DateTime dtcom = Convert.ToDateTime(comdate);
                    if (datetime__1 < dtcom)
                    {
                        e.Row.BackColor = Color.Red;
                    }
                    if (datetime__1 == dtcom)
                    {
                        e.Row.BackColor = Color.LightCoral;
                    }
                    if (datetime__1 > dtcom)
                    {
                        e.Row.BackColor = Color.LightBlue;
                    }
                }
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 5, 2017 3:14 PM

All replies

  • User3690988 posted

    What is the data element myHour?  Is it a Date and time?

    What are you trying to accomplish?  It appears that you are trying to see if a time is greater than or equal to 12:00am?  Which seems to me that anytime would be.

    If you want to compare 2 times, I would look into using TimeSpan.Compare

    Wednesday, October 4, 2017 6:28 PM
  • User-1740043572 posted

    protected void grdNon_Approval_Insu_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    try
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    Label lblService_No = (Label)e.Row.FindControl("lblService_No");
    Label lblService_Date = (Label)e.Row.FindControl("lblService_Date");

    if(lblService_No.Text != string.Empt)

    {

    e.Row.BackColor = Color.RosyBrown;
    lblService_No.ForeColor = Color.RosyBrown;

    }
    }
    }
    catch (Exception ex)
    {
    Messagebox(ex.ToString());
    }
    }

    Thursday, October 5, 2017 3:50 AM
  • User1872364149 posted

    What is the data element myHour?  Is it a Date and time?

    What are you trying to accomplish?  It appears that you are trying to see if a time is greater than or equal to 12:00am?  Which seems to me that anytime would be.

    If you want to compare 2 times, I would look into using TimeSpan.Compare

    Thank you for reply.

    The data element myHour is varchar on the db.

    Thursday, October 5, 2017 7:58 AM
  • User3690988 posted

    Your issue is that you are trying to compare two strings in your if statement.  c# does not allow you to do this.  If your myHour is really a time, stored in a varchar, and you want to compare that, as I said I would use the TimeSpan.Compare.  An example:

          string strDate1 = "05/05/2017 03:00:27";
          string strDate2 = "04/05/2017 06:00:27";
          dynamic date1 = Convert.ToDateTime(strDate1);
          dynamic date2 = Convert.ToDateTime(strDate2);
          // Compare Dates
          if (date1 <= date2)
          {
            lblMsg.Text = strDate1 + " <= " + strDate2;
          }
          else
          {
            lblMsg.Text = strDate1 + " > " + strDate2;
          }
    
          // Compare Times
          int tspanCompareResult = TimeSpan.Compare(date1.TimeOfDay, date2.TimeOfDay);
          switch (tspanCompareResult)
          {
            case -1:
              lblMsg.Text += "<br />Time: " + strDate1 + " < " + strDate2;
              break;
            case 0:
              lblMsg.Text += "<br />Time: " + strDate1 + " = " + strDate2;
              break;
            case 1:
              lblMsg.Text += "<br />Time: " + strDate1 + " > " + strDate2;
              break;
          }

    Thursday, October 5, 2017 11:20 AM
  • User-1838255255 posted

    Hi cms9651,

    According to your description and needs, you want to compare datetime, then change the gridview row backgrond color, i make a sample, please check:

    Sample Code:

    <asp:GridView ID="GridView2" runat="server"
                    EnableTheming="True" AutoGenerateColumns="false" OnRowDataBound="GridView2_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Item" HeaderText="Item" />
                        <asp:BoundField DataField="Rmk_Dt" HeaderText="Rmk_Dt" />
                    </Columns>
    </asp:GridView>
    
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[2] {
                new DataColumn("Item", System.Type.GetType("System.String")),
                new DataColumn("Rmk_Dt", System.Type.GetType("System.DateTime"))
            });
                    dt.Rows.Add("Shirt", DateTime.Parse("9/3/2017 20:00:00"));
                    dt.Rows.Add("Jeans", DateTime.Parse("10/15/2017 20:00:00 "));
                    dt.Rows.Add("Trousers1", DateTime.Parse("9/29/2017 20:00:00"));
                    dt.Rows.Add("Tie", DateTime.Parse("10/5/2017 23:30:00"));
                    dt.Rows.Add("Trousers2", DateTime.Parse("10/10/2017 20:00:00"));
                    dt.Rows.Add("Cap", DateTime.Parse("10/4/2017 20:00:00"));
                    dt.Rows.Add("Cap2", DateTime.Parse("10/15/2017 20:00:00"));
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }
            }
            string comdate = "10/05/2017 23:00:27";
            protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DateTime datetime__1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Rmk_Dt").ToString());
                    DateTime dtcom = Convert.ToDateTime(comdate);
                    if (datetime__1 < dtcom)
                    {
                        e.Row.BackColor = Color.Red;
                    }
                    if (datetime__1 == dtcom)
                    {
                        e.Row.BackColor = Color.LightCoral;
                    }
                    if (datetime__1 > dtcom)
                    {
                        e.Row.BackColor = Color.LightBlue;
                    }
                }
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 5, 2017 3:14 PM