当DropDownList筛选器的值为而不是ALL时,如何删除GridView中的整个第一列? [英] How to remove the whole first column in the GridView when the value of the DropDownList Filter is rather than ALL?
问题描述
我有以下复杂的asp.net代码结构:
1. DropDownList作为过滤器
2.转发器
3.在Repeater内部:我有HiddenField和GridView
我想将过滤器"的值设置为全部"时,应该删除第一列.问题是我正在使用一个StoredProcedure,它负责生成三个GridView,因此我将GridView放在Repeater内.
ASP.NET代码:
I have the following complicated asp.net code structure:
1. DropDownList as a Filter
2. Repeater
3. And inside the Repeater: I have HiddenField and GridView
I want when the value of Filter is setting to All, the fist column should be removed. The problem is I am using a StoredProcedure that takes care for generating three GridViews and this way I put the GridView inside the Repeater.
The ASP.NET code:
<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>
<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>
<div style="width:700px; overflow:auto; overflow-y:hidden;">
<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>
</div>
<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>
背后的代码:
The code-behind:
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;
// }
// }
// }
// }
// }
//}
//protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
//{
// if (ddlDivision.SelectedItem.Text == "All")
// {
// GridView1.Columns[0].Visible = true;
// }
// else
// {
// GridView1.Columns[0].Visible = false;
// }
//}
//This method is for deleting the first column in the GridView
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// e.Row.Cells[0].Visible = false; // hides the first column
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//GridView GridView1 = (GridView)sender;
//foreach (GridViewRow gvr in GridView1.Rows)
//{
// if (ddlDivision.SelectedValue != "All")
// {
// gvr.Cells[0].Visible = false;
// }
//}
}
//This function is for checking each cell in each row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//var gv = sender as GridView;
//if (ddlDivision.SelectedValue == "All")
// gv.Columns[0].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell c in e.Row.Cells)
{
// Check if the cell vlaue = Yes
// if it is Yes, the cell will be colored with Light Green
if (c.Text.Contains(", Yes"))
{
c.BackColor = System.Drawing.Color.LightGreen;
c.Text = "•";
}
else if (c.Text.Contains(", NO"))
{
c.Text = "";
}
}
}
//The following is for changing the color of headers in each GridView based on the value of the HiddenFild
//BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database
else if (e.Row.RowType == DataControlRowType.Header)
{
switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
{
case "1":
for (int i = 4; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
break;
case "2":
for (int i = 4; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
break;
case "3":
for (int i = 4; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
break;
}
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
var gv = sender as GridView;
if (gv.Rows.Count > 0)
{
gv.UseAccessibleHeader = true;
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
我做了很多尝试,但都失败了,正如您在上面的背后代码中所看到的.上一次尝试是:
I did many tries but I failed in all of them as you see in the above behind-code. The last try was:
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;
}
}
}
}
}
}
我失败了**有人可以帮我解决这个问题吗?**
and I failed. **Could anyone help me with this issue?**
推荐答案
ConnectionStrings:testConnectionString%> SelectCommandType ="StoredProcedure" SelectCommand ="kbiReport" FilterExpression ="[DivisionName]之类的"{0}%""> < FilterParameters> < asp:ControlParameter ControlID ="ddlDivision" Name ="DivisionName" PropertyName ="SelectedValue" Type ="String"/> </FilterParameters> < SelectParameters> <%-ControlParameter链接到上面的HiddenField,以基于不同的值生成不同的GridView 的GroupID-%> < asp:ControlParameter ControlID ="HiddenField1" Name ="GroupID" PropertyName ="Value"/> </SelectParameters> </asp:SqlDataSource> < div style ="width:700px;溢出:自动;溢出y:隐藏;"> < asp:GridView ID ="GridView1" runat ="server" AllowSorting =真" 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"/> <列> </列> < 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> </div> < br/> </ItemTemplate> </asp:Repeater> < asp:SqlDataSource ID ="SqlDataSource1" runat =服务器" 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> <div style="width:700px; overflow:auto; overflow-y:hidden;"> <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> </div> <br /> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%
ConnectionStrings:testConnectionString%>" SelectCommand =从课程中选择DISTINCT GroupID"> </asp:SqlDataSource> <%-按部门筛选-%> < asp:SqlDataSource ID ="sqlDataSourceDivision" runat =服务器" 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>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [DivisionName] FROM [Divisions]"></asp:SqlDataSource>
背后的代码:
The code-behind:
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;
// }
// }
// }
// }
// }
//}
//protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
//{
// if (ddlDivision.SelectedItem.Text == "All")
// {
// GridView1.Columns[0].Visible = true;
// }
// else
// {
// GridView1.Columns[0].Visible = false;
// }
//}
//This method is for deleting the first column in the GridView
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// e.Row.Cells[0].Visible = false; // hides the first column
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//GridView GridView1 = (GridView)sender;
//foreach (GridViewRow gvr in GridView1.Rows)
//{
// if (ddlDivision.SelectedValue != "All")
// {
// gvr.Cells[0].Visible = false;
// }
//}
}
//This function is for checking each cell in each row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//var gv = sender as GridView;
//if (ddlDivision.SelectedValue == "All")
// gv.Columns[0].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell c in e.Row.Cells)
{
// Check if the cell vlaue = Yes
// if it is Yes, the cell will be colored with Light Green
if (c.Text.Contains(", Yes"))
{
c.BackColor = System.Drawing.Color.LightGreen;
c.Text = "•";
}
else if (c.Text.Contains(", NO"))
{
c.Text = "";
}
}
}
//The following is for changing the color of headers in each GridView based on the value of the HiddenFild
//BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database
else if (e.Row.RowType == DataControlRowType.Header)
{
switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
{
case "1":
for (int i = 4; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
break;
case "2":
for (int i = 4; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
break;
case "3":
for (int i = 4; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
break;
}
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
var gv = sender as GridView;
if (gv.Rows.Count > 0)
{
gv.UseAccessibleHeader = true;
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
我做了很多尝试,但都失败了,正如您在上面的背后代码中所看到的.上一次尝试是:
I did many tries but I failed in all of them as you see in the above behind-code. The last try was:
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;
}
}
}
}
}
}
我失败了**有人可以帮我解决这个问题吗?**
and I failed. **Could anyone help me with this issue?**
这篇关于当DropDownList筛选器的值为而不是ALL时,如何删除GridView中的整个第一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!