How to hide a TemplateField column in a GridView

前端 未结 9 1818
遇见更好的自我
遇见更好的自我 2020-12-03 04:13

How can I hide a TemplateField column in a GridView?

I tried the following:



        
相关标签:
9条回答
  • 2020-12-03 04:54
    protected void gvLogMessageDetail_RowDataBound(object sender, GridViewRowEventArgs e)  
        { 
          if (e.Row.RowType == DataControlRowType.Header)   
            {  
                if (rdlForImportOrExport.SelectedIndex == 1)  
                {  
                    e.Row.Cells[3].Visible = false;  
                    e.Row.Cells[4].Visible = false;  
                    e.Row.Cells[5].Visible = false;  
                }  
                else  
                {  
                    e.Row.Cells[3].Visible = true;  
                    e.Row.Cells[4].Visible = true;  
                    e.Row.Cells[5].Visible = true;  
                }  
            }    
            if (e.Row.RowType == DataControlRowType.DataRow) //skip header row  
            {  
                try  
                {  
                    if (rdlForImportOrExport.SelectedIndex == 1)  
                    {  
                        e.Row.Cells[3].Visible = false;  
                        e.Row.Cells[4].Visible = false;  
                        e.Row.Cells[5].Visible = false;  
                    }  
                    else  
                    {  
                        e.Row.Cells[3].Visible = true;  
                        e.Row.Cells[4].Visible = true;  
                        e.Row.Cells[5].Visible = true;  
                    }  
                    }  
                catch  
                {  
                    ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT   LANGUAGE='javascript'>alert('There is binding problem in child grid.');</script>");  
                }  
            }  
        }  
    
    0 讨论(0)
  • 2020-12-03 04:55

    try this

    .hiddencol
        {
            display:none;
        }
        .viscol
        {
            display:block;
        }
    

    add following code on RowCreated Event of GridView

    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             e.Row.Cells[0].CssClass = "hiddencol";
         }
         else if (e.Row.RowType == DataControlRowType.Header)
         {
             e.Row.Cells[0].CssClass = "hiddencol";
         }
    }
    
    0 讨论(0)
  • 2020-12-03 04:57
    GridView1.Columns[columnIndex].Visible = false;
    
    0 讨论(0)
  • 2020-12-03 05:04

    This can be another way to do it and validate nulls

    DataControlField dataControlField = UsersGrid.Columns.Cast<DataControlField>().SingleOrDefault(x => x.HeaderText == "Email");
                if (dataControlField != null)
                    dataControlField.Visible = false;
    
    0 讨论(0)
  • 2020-12-03 05:08
    For Each dcfColumn As DataControlField In gvGridview.Columns
        If dcfColumn.HeaderText = "ColumnHeaderText" Then
            dcfColumn.Visible = false                    
        End If
    Next
    
    0 讨论(0)
  • 2020-12-03 05:17

    If appears to me that rows where Visible is set to false won't be accessible, that they are removed from the DOM rather than hidden, so I also used the Display: None approach. In my case, I wanted to have a hidden column that contained the key of the Row. To me, this declarative approach is a little cleaner than some of the other approaches that use code.

    <style>
       .HiddenCol{display:none;}                
    </style>
    
    
     <%--ROW ID--%>
          <asp:TemplateField HeaderText="Row ID">
           <HeaderStyle CssClass="HiddenCol" />
           <ItemTemplate>
           <asp:Label ID="lblROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:Label>
           </ItemTemplate>
           <ItemStyle HorizontalAlign="Right" CssClass="HiddenCol" />
           <EditItemTemplate>
           <asp:TextBox ID="txtROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:TextBox>
           </EditItemTemplate>
           <FooterStyle CssClass="HiddenCol" />
          </asp:TemplateField>
    
    0 讨论(0)
提交回复
热议问题