This is a migrated thread and some comments may be shown as answers.

Null Reference Exception while doing conditional formatting for radgrid

2 Answers 39 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bernard
Top achievements
Rank 2
Bernard asked on 06 Aug 2013, 10:16 AM

I am currently attempting to modify the way my radgrid displays data by highlighting specific rows which has went over my "overdue" limit. here's a snippet of the methods i am using..

ASPX

<telerik:GridBoundColumn DataField="TimeCreated" HeaderText="Posted On" ReadOnly="true" UniqueName="TimeCreated"/>

C# Method1

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
 
        string time = dataItem["TimeCreated"].Text;
        DateTime timePosted = DateTime.Parse(time);
        TimeSpan allowance = new TimeSpan(0, 25, 0);
        DateTime overdue = timePosted.Add(allowance);
 
        if (DateTime.Now > overdue)
        {
            dataItem.ForeColor = System.Drawing.Color.LightPink;
            dataItem.Font.Bold = true;
        }
    }

C# Method 2

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        TimeSpan allowance = new TimeSpan(0, 25, 0);
 
        if (DateTime.Compare(Convert.ToDateTime(dataItem["TimeCreated"].Text).Add(allowance), DateTime.Now) > 0)
        {
            dataItem.ForeColor = System.Drawing.Color.LightPink;
            dataItem.Font.Bold = true;
        }
    }

C# Method 3

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        TimeSpan allowance = new TimeSpan(0, 25, 0);
 
 
        if (DateTime.Parse(dataItem["TimeCreated"].Text).Add(allowance) > DateTime.Now )
        {
            dataItem.ForeColor = System.Drawing.Color.LightPink;
            dataItem.Font.Bold = true;
        }
    }

All of these methods produces the same exact NullReferenceException which is unfathomable to me, i hope someone could shed some light on this. thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Aug 2013, 12:13 PM
Hello,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = e.Item as GridDataItem;
 
            // Your conditional formatting code should come here
       }
 }

Note : You forgot to add "IF" condition in your event.

Thanks,
Jayesh Goyani
0
Bernard
Top achievements
Rank 2
answered on 06 Aug 2013, 02:07 PM
Thanks a lot Jayesh, you pretty much nailed it. I missed out that if statement.
Tags
Grid
Asked by
Bernard
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Bernard
Top achievements
Rank 2
Share this question
or