c# - How to hide column and access it programatically in Gridview? -


i have gridview bonded totally programatically . want hide columns , rich them show them in label.

here gridview :

<gridview id=gridview1 runat="server" ></gridview> 

i want hide empid , unitid . , want show them in label on front-end side

empid  empname unitid  unitname     --------------------------------  1      jack     4      myunit 

i trying use code , not working gives me error

 if (gridforunits.columns.count > 1)         {            gridforunits.columns[1].visible = false;            //gridforunits.columns[1].visible = false;         } 

any appreciate

thanks

if want hide column base on logic, want use rowdatabound.

it bit easy maintain in future.

here sample. can hide or show whatever columns like.

enter image description here

<asp:gridview id="gridview1" runat="server" onrowdatabound="gridview1_rowdatabound"      autogeneratecolumns="false">     <columns>         <asp:boundfield headertext="empid" datafield="empid" />         <asp:templatefield headertext="empname" >             <itemtemplate>                 <asp:label runat="server" id="empnamelabel"                      text='<%# eval("empname") %>' />             </itemtemplate>         </asp:templatefield>         <asp:boundfield headertext="unitid" datafield="unitid" />         <asp:templatefield headertext="unitname" >             <itemtemplate>                 <asp:label runat="server" id="unitnamelabel"                      text='<%# eval("unitname") %>' />             </itemtemplate>         </asp:templatefield>     </columns> </asp:gridview>   public class employee {     public int empid { get; set; }       public string empname  { get; set; }       public int unitid  { get; set; }     public string unitname { get; set; }   }  protected void page_load(object sender, eventargs e) {     gridview1.datasource = new list<employee>     {     new employee { empid = 1, empname = "one", unitid = 100, unitname = "one hundred"},     new employee { empid = 2, empname = "two", unitid = 200, unitname = "two hundred"},     new employee { empid = 3, empname = "three", unitid = 300, unitname = "three hundred"},     new employee { empid = 4, empname = "four", unitid = 400, unitname = "four hundred"}     };     gridview1.databind(); }   protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) {     if (e.row.rowtype == datacontrolrowtype.datarow)     {         var employee = e.row.dataitem employee;         var empnamelabel = e.row.findcontrol("empnamelabel") label;         var unitnamelabel = e.row.findcontrol("unitnamelabel") label;          if (employee.unitid == 200)         {             empnamelabel.visible = false;             unitnamelabel.visible = false;         }     } } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -