我有一个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
}
}
答案 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;
}
}
已编辑:假设您的DataSource
为DataTable
,您可以执行以下操作:
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;
}
}
}