禁用网格视图中的最后一行

时间:2012-11-08 09:20:35

标签: c# asp.net gridview

我有一个gridview,我想要禁用它的最后5行我怎么能这样做??下面的代码不能正常工作

  protected void gview_RowDataBound(object sender, GridViewRowEventArgs e)
  {
     if (e.Row.RowType == DataControlRowType.DataRow)
    {
          e.Row.Enabled = e.Row.RowIndex <= 5; //for disabling last 4 rows 
    }
  }

2 个答案:

答案 0 :(得分:1)

protected void gview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridView grid = sender as GridView;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Enabled = grid.Rows.Count - e.Row.RowIndex > 4;
    }
}

已编辑:假设您的DataSourceDataTable,您可以执行以下操作:

protected void gview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Enabled = dataTable.Rows.Count - e.Row.RowIndex > 4;
    }
}

答案 1 :(得分:0)

我假设您将数据集绑定为gridview的数据源,因此在RowDataBound绑定中添加以下代码:

public static int count=0;
protected void grdview1_RowDataBound()
 {
   for(int i=0;i< ds.table[0].rows.count;i++) 
    {
      count++;
      if(count>(ds.table[0].rows.count-5))
      {
          e.Row.Enabled = false;
      }  
   }
}