[英]Changing the property of a button in a specific row within a gridview (ASP.NET)
[英]ASP.NET Gridview within Gridview but without the header for each row
我嘗試在asp.net的gridview中使用gridview,這就是我所做的
這樣做的問題是,如果行太多,則標題變得煩人。 我希望內部gridview共享如下通用標題:
在ASP.NET中有可能嗎? 怎么樣?
示例gridview標記
<asp:GridView ID="ItemsGridView" runat="server" AutoGenerateColumns="False" BackColor="White" CssClass="gridviews" Width="600px"
BorderColor="Black" BorderStyle="Ridge" BorderWidth="1px" CellPadding="5" OnRowCommand="ItemsGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="" SortExpression="DepartmentId">
<ItemTemplate>
<asp:LinkButton CommandName="GoDet" Font-Underline="false" ForeColor="#676771" Font-Bold="True" ID="linkDepartmentId" CommandArgument='<%# Eval("DepartmentId") %>'
Text="View" runat="server" />
</ItemTemplate>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-VerticalAlign="Middle">
<ItemStyle VerticalAlign="Middle" Width="300px" Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Sub-Groups">
<ItemTemplate>
<asp:GridView ID="SubGroupGridView" runat="server" AutoGenerateColumns="False" BackColor="White" CssClass="subgridviews"
DataSource='<%# Bind("SubGroupees") %>' BorderStyle="Outset" BorderWidth="1px" CellPadding="3" CellSpacing="3" ShowHeader="False" GridLines="Vertical" OnRowCommand="SubGroupGridView_RowCommand">
<Columns>
<asp:BoundField DataField="Name">
<ItemStyle Width="180px" />
</asp:BoundField>
<asp:TemplateField HeaderText="" SortExpression="SubGroupId">
<ItemTemplate>
<asp:LinkButton CommandName="GoSubDet" ID="linkSubGroupId" Font-Underline="false" ForeColor="#676771" Font-Bold="True"
CommandArgument='<%# Eval("SubGroupId") %>' Text="Edit" runat="server" />
</ItemTemplate>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" SortExpression="SubGroupId">
<ItemTemplate>
<asp:LinkButton CommandName="DelSub" ID="linkDelSub" Font-Underline="false" ForeColor="#676771" Font-Bold="True" CommandArgument='<%# Eval("SubGroupId") %>'
Text="Delete" runat="server" />
</ItemTemplate>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" SortExpression="DepartmentId">
<ItemTemplate>
<asp:LinkButton CommandName="AddSub" ID="linkSubGroupId" Font-Underline="false" ForeColor="#676771" Font-Bold="True" CommandArgument='<%# Eval("DepartmentId") %>'
Text="Add Sub-Group" runat="server" />
</ItemTemplate>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" SortExpression="DepartmentId">
<ItemTemplate>
<asp:LinkButton CommandName="DelItem" ID="linkDelDepartmentId" Font-Underline="false" ForeColor="#676771" Font-Bold="True" CommandArgument='<%# Eval("DepartmentId") %>'
Text="Delete" runat="server" />
</ItemTemplate>
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
您可以為父GridView中的事件使用OnRowDataBound
事件,並檢查嵌套的GridView是否為第一個,並且僅在第一個中顯示標題。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the nested grid in the current row with findcontrol
GridView gridView = e.Row.FindControl("nestedGridView") as GridView;
//check if it is the first nested gridview and show/hide the header
if (e.Row.RowIndex == 0)
{
gridView.ShowHeader = true;
}
else
{
gridView.ShowHeader = false;
}
//fill the nested grid with data
gridView.DataSource = dataSource;
gridView.DataBind();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.