如何隐藏将使用StoredProcedure生成的GridView中的第一列
本文关键字:GridView 一列 StoredProcedure 隐藏 何隐藏 | 更新日期: 2023-09-27 18:20:08
我在Repeater控件内使用GridView 生成三个GridView,它们之间有3个相似的列和不同数量的不同列。一切都很顺利。我只是面临一个过滤结果的问题。我有一个基于部门名称的过滤器作为下拉列表。此筛选器显示以下值:All和从数据库中检索的分部名称。当我根据其中一个分区筛选结果时,我没有显示分区名称,而是以编程方式隐藏"分区"列。
现在,我希望当filter的值为All时显示Division列,而不是All值时显示该列。那么如何做到这一点呢?
我的ASP.NET代码:
<asp:DropDownList ID="ddlDivision" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="sqlDataSourceDivision" DataTextField="DivisionName"
DataValueField="DivisionName"
Width="275px" EnableViewState="False">
<asp:ListItem Value="%">All</asp:ListItem>
</asp:DropDownList>
<strong> Search </strong>
<input id=id_search type=text placeholder="Search">
<br /> <br />
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="kbiReport"
FilterExpression="[DivisionName] like '{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="DivisionName"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
<SelectParameters>
<%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values
of GroupID--%>
<asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True"
CellPadding="3"
DataSourceID="SqlDataSource1"
ClientIDMode="Static" class="fixedTables" Width="600" AutoGenerateColumns="true"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound" OnPreRender="GridView1_PreRender" OnRowCreated="GridView1_RowCreated"
OnDataBound="GridView1_DataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black"/>
<Columns>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT DISTINCT GroupID FROM courses">
</asp:SqlDataSource>
<%--Filtering by Division--%>
<asp:SqlDataSource ID="sqlDataSourceDivision" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [DivisionName] FROM [Divisions]"></asp:SqlDataSource>
隐藏第一列的代码是:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
GridView gv = e.Item.FindControl("GridView1") as GridView;
if (gv != null)
{
gv.DataBind();
if (ddlDivision.SelectedValue != "ALL")
{
if (gv.Columns.Count > 0)
gv.Columns[0].Visible = false;
else
{
gv.HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in gv.Rows)
{
gvr.Cells[0].Visible = false;
}
}
}
}
}
}
您应该尝试在databind()调用之后隐藏该列。。
GridView1.DataBind();
if (GridView1.Columns.Count > 0)
GridView1.Columns[0].Visible = false;
else
{
GridView1.HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[0].Visible = false;
}
}
编辑:
void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
GridView gv = e.Item.FindControl("GridView1") as GridView;
if (gv != null )
{
gv.DataBind();
if(ddlMyList.SelectedText != "ALL")
{
if (gv.Columns.Count > 0)
gv.Columns[0].Visible = false;
else
{
gv.HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in gv.Rows)
{
gvr.Cells[0].Visible = false;
}
}
}
}
}
}
public protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; // hides the first column
}