我目前正在使用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>
答案 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>