隐藏DataGridView列

时间:2018-01-04 19:12:54

标签: javascript c# jquery asp.net datagridview

我目前正在使用HeaderText来显示/隐藏asp:GridView

中的列

现在我需要没有HeaderText的显示文字,所以我不知道如何根据按钮点击重构这种语法来显示/隐藏列。

这是我目前的C#和HTML - >我将如何更改它以便它仍然可以在没有HeaderText值设置的情况下工作?

   private int GetColumnIndex(GridView grid, string ColName)
{
    foreach (DataControlField col in grid.Columns)
    {
        if (col.HeaderText.ToLower().Trim() == ColName.ToLower().Trim())
        {
            return grid.Columns.IndexOf(col);
        }
    }
    return -1;
}       

protected void btnS_Click(object sender, EventArgs e)
{       
    var columnname = string.Empty;
    foreach (System.Web.UI.WebControls.ListItem item in cbxSponsorLevel.Items)
    {
        columnname = item.Text;
        var index = GetColumnIndex(GridView1, columnname);
        string number = index.ToString();
        if (index > 0)
        {
            GridView1.Columns[index].Visible = item.Selected;
        }
    }
}

<div id="dgv">
    <asp:GridView ID="GridView1" runat="server" Font-Size="9px" CssClass="Grid" AutoGenerateColumns="false" >
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:BoundField DataField="Sport" HeaderText="Sport"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
    <asp:BoundField DataField="Event Number" HeaderText="Event"> <ItemStyle HorizontalAlign="Center" /></asp:BoundField>
    <asp:BoundField DataField="Score" HeaderText="Score"> <ItemStyle HorizontalAlign="Center" /></asp:BoundField>
</Columns>        
</asp:GridView></div>

1 个答案:

答案 0 :(得分:1)

CSS:

<style type="text/css">
    .hidden-field
        {
           display:none;
        }
 </style>

HTML:

<asp:BoundField DataField="Sport" HeaderText="Sport" HeaderStyle-CssClass="hidden-field">
    <ItemStyle HorizontalAlign="Center" />
</asp:BoundField>