1

这对我来说没有意义:如果我运行这段代码:

protected void viewStoryTime_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var obj = ((DataRowView)e.Row.DataItem)["ActivityDate"];
        if (DateTime.Parse(obj.ToString()) < DateTime.Now.StartOfWeek(DayOfWeek.Monday))
        {
            System.Diagnostics.Debug.WriteLine(e.Row.RowIndex);
        }
    }
}

我的输出是:

4

现在,如果我尝试使用它来清除单元格:

protected void viewStoryTime_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var obj = ((DataRowView)e.Row.DataItem)["ActivityDate"];
        if (DateTime.Parse(obj.ToString()) < DateTime.Now.StartOfWeek(DayOfWeek.Monday))
        {
            viewStoryTime.Rows[e.Row.RowIndex].Cells[3].Controls.Clear();
        }
    }
}

我得到:

在 mscorlib.dll 中发生了“System.ArgumentOutOfRangeException”类型的第一次机会异常

此外,如果我手动输入 4,则会出现此异常:

viewStoryTime.Rows[e.Row.RowIndex].Cells[3].Controls.Clear();

但是,如果我输入 3,它会直接从列中删除它应该在该列之前的控件:

在此处输入图像描述

为什么我会收到此异常,我该怎么办?

4

1 回答 1

1

Based on everything you've tried, the only way you're getting this exception is for e.Row.RowIndex to be out of range:

viewStoryTime.Rows[e.Row.RowIndex]...

However, it's unclear to me why you don't simply do this:

e.Row.Cells[3].Controls.Clear();

You could have issues accessing the rows on viewStoryTime depending on where the page is in the event life cycle.

于 2013-07-11T19:08:45.467 回答