为radgrid执行条件格式化时出现Null引用异常

时间:2013-08-06 09:36:06

标签: telerik radgrid

我目前正在尝试修改我的radgrid显示数据的方式,方法是突出显示已超过“逾期”限制的特定行。这是我正在使用的方法的片段......

ASPX

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

C#方法1

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#方法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#方法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;
        }
    }

所有这些方法都产生了同样精确的NullReferenceException,这对我来说是不可思议的,我希望有人可以对此有所了解。感谢

1 个答案:

答案 0 :(得分:0)

似乎我错过了一个if语句来绑定我想要的列的网格行单元格......

这是正确的方法

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem) // bind the intended column cells
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            TimeSpan allowance = new TimeSpan(0, 25, 0);

            if (DateTime.Parse(dataItem["TimeCreated"].Text).Add(allowance) < DateTime.Now)
            {
                dataItem.BackColor = System.Drawing.Color.LightPink;
                dataItem.Font.Bold = true;
            }
        }
    }