0

我想更改 gridview 中第一行的 CSS 样式:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridViewRow firstRow = hoursReportGridView.Rows[0];
        firstRow.CssClass = "firstRow";
    }
}

我收到此错误:索引超出范围。必须是非负数且小于集合的大小。

在每种情况下,都会返回多行,所以我不明白这个问题在此处输入图像描述 '

4

3 回答 3

3

我假设第一行存在于GridView.Rows after RowDataBound中。所以你以后可以访问它。所以我会DataBound改用。请注意,您还设置了每一行的第一行,因为RowDataBound为网格中的每一行触发。

protected void hoursReportGridView_DataBound(object sender, EventArgs e)
{
    if(this.hoursReportGridView.Rows.Count > 0)
        hoursReportGridView.Rows[0].CssClass = "firstRow";
}

另一种选择是使用GridViewRow.RowIndex

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if(e.Row.RowIndex == 0)
            e.Row.CssClass = "firstRow";
    }
}
于 2013-09-20T15:08:50.227 回答
1

试试这个:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex == 0)
        {
            e.Row.CssClass = "firstRow";

    }
}
于 2013-09-20T15:04:52.833 回答
1

为什么要对每一行的数据绑定执行此操作?

只需这样做Page_Load

// Run this after any binding calls, obviously
if(hoursReportGridView.Rows.Count > 0)
{
    hoursReportGridView.Rows[0].CssClass = "firstRow";
}
于 2013-09-20T15:08:54.780 回答