0

我目前正在尝试通过突出显示超出我的“过期”限制的特定行来修改我的 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,这对我来说是深不可测的,我希望有人能对此有所了解。谢谢

4

1 回答 1

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;
            }
        }
    }
于 2013-08-06T14:10:03.987 回答