VxChemical
VxChemical

Reputation: 23

Change gridview row color based on DateTime.Compare

I am trying to change the color of my gridview row if its past the current date. I have looked around and conjured up something that seems like it should work. However it doesn't. Why?

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

Source Error:

Line 114: { Line 115:
Line 116: DateTime dt = Convert.ToDateTime(((DataRowView)e.Row.DataItem)["ExpiryDate"]); Line 117: string Test = DateTime.Compare(DateTime.Now,dt).ToString(); Line 118:

Here is my code

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{

    DateTime dt = Convert.ToDateTime(((DataRowView)e.Row.DataItem)["ExpiryDate"]); 
    string Test = DateTime.Compare(DateTime.Now,dt).ToString();

    if (Test == "0")
    {
        e.Row.BackColor = System.Drawing.Color.Red;
    }
    else
    {
        e.Row.BackColor = System.Drawing.Color.White;
    }
}

Upvotes: 1

Views: 2011

Answers (2)

VxChemical
VxChemical

Reputation: 23

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        onrowdatabound="GridView1_RowDataBound" >
                <Columns>
                    <asp:TemplateField HeaderText="ExpiryDate">
                        <ItemTemplate>
                            <asp:Label ID="lblExpiryDate" runat="server"
                                Text='<%# DataBinder.Eval(Container, "DataItem.ExpiryDate") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    ......                       
                </Columns>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string v_ExpiryDate = (string)DataBinder.Eval(e.Row.DataItem, "ExpiryDate");
            string Test = DateTime.Compare(DateTime.Now,Convert.ToDateTime(v_ExpiryDate)).ToString();
            if (Test == "0")
             {
                e.Row.BackColor = System.Drawing.Color.Red;
             }
           else
             {
                e.Row.BackColor = System.Drawing.Color.White;
             }
        }

    }

Thanks to

Ashim Chatterjee

Upvotes: 0

Felipe Oriani
Felipe Oriani

Reputation: 38598

You could check some objects and use some safe casts to check if everything is fine before using it.

The conversion could be done with a date time format, for sample dd/MM/yyyy or mm/DD/yyyy and try to extract the date with DateTime.TryParseExact. I am not sure about your date time format, but, you could try something like this (look the comments):

private CultureInfo enUS = new CultureInfo("en-US"); 
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // check if it is a row that contains data
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // convert the dataItem to your datasource type with a safe cast
        DataRowView row = e.Row.DataItem as DataRowView;

        // check if the conversion was succeed
        if (row != null)
        {
            // check if the date column is not null
            if (row["ExpiryDate"] != null)
            {
                // try to convert the string into a datetime with a specific format (i am not sure about the date format you are using)
                DateTime dt;
                if (DateTime.TryParseExact(row["ExpiryDate"], "mm/DD/yyyy", enUS, DateTimeStyles.None, out dt)) 
                {
                    // conversion looks ok, do your task                    
                    int compareResult = DateTime.Compare(DateTime.Now, dt);
                    e.Row.BackColor = compareResult == 0 ? System.Drawing.Color.Red : System.Drawing.Color.White; 
                }
            }       
        }
    }
}

Upvotes: 1

Related Questions