Wednesday, May 9, 2012

Asp:GridView Hide Colums example

in case u ever tried to do something like this: dgvUsersCompanies.Columns[0].Visible = false;
u've got an ArgumentOutOfRangeException. hell i dont know why, it seems our columns r just not stored there
so... i found 2 options:
with the dgvUsersCompanies_RowDataBound event u cant alter the Cells[x].Visible and that is cuz he (the event) dont have access columns but this one has :)
protected void dgvUsersCompanies_RowCreated(object sender, GridViewRowEventArgs e) { e.Row.Cells[2].Visible = false; }
Remember that in the user page the column wont be at all

if u want the user to have the col but unseen what u can do (with either events) is
protectedvoid dgvUsersCompanies_RowDataBound(object sender, GridViewRowEventArgs e)
{  e.Row.Cells[2].Attributes["style"] = "display: none;";  }

*Defenetly not use visibility cuz u'll still see the td but not the inside
aslo dont do the check for DataControlRowType.DataRow cuz u'll see the headers

No comments:

Post a Comment