Search This Blog

Tuesday, March 17, 2009

How To do invisible a column of Gridview

Sometimes we need to do invisible some particular column of a gridview for some cases.This can do as follows.
In below example admin can Only view column 2 and 21.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(user != "Admin")
{
if(e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[2].Visible=false;
e.Row.Cells[21].Visible=false;
}
else if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Visible=false;
e.Row.Cells[21].Visible=false
}
}

}

7 comments:

  1. Hello Rakesh
    write below line just above
    bool isAllowedToEditJob = Convert.ToBoolean(Session["EditJob"]);


    and replace line if(user != "Admin") with

    if(isAllowedToEditJob == false)

    ReplyDelete
  2. i have one gridview.inside that gridview there is one CommandField as
    ShowEditButton="True" HeaderText="Edit"
    and ItemTemplete
    as Delete


    now my condition is that we have saveral users.
    some users has rights to edit or delete but some has not right to delete or edit.
    so those users who has not rights to edit and delete job,should not view edit or delete button.

    ReplyDelete
  3. hello Arvind,
    Edit is working perfectly but delete button is not working

    ReplyDelete
  4. hello
    what is the column index of this two field?

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete