在GridView中隐藏基于另一列的单元格

时间:2013-11-01 14:28:49

标签: c# asp.net gridview visual-studio-2012

我有以下GridView:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="InvoiceID" HeaderText="SysInvoiceID" ReadOnly="True" SortExpression="SysInvoiceID" />
                <asp:BoundField DataField="BillMonth" HeaderText="BillMonth" SortExpression="BillMonth" />
                <asp:BoundField DataField="InvoiceDate" HeaderText="InvoiceDate" ReadOnly="True" SortExpression="InvoiceDate" />
                <asp:BoundField DataField="InvoiceNumber" HeaderText="InvoiceNumber" SortExpression="InvoiceNumber" />
                <asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
                <asp:BoundField DataField="VAT" HeaderText="VAT" SortExpression="VAT" />
                <asp:BoundField DataField="Gross" HeaderText="Gross" SortExpression="Gross" />
                <asp:ButtonField CommandName="ViewInvoice"  HeaderText=" " ShowHeader="True" Text="View" />
            </Columns>
        </asp:GridView>

最后一列(ButtonField)是我自己创建的,只是为了在每一行中包含文本“View”,点击后会显示PDF发票。

我不确定这是否可行,但我想知道是否可以为该列或其他内容添加某种验证,以便如果'InvoiceID'列为空,则为'查看'链接在相应的行上不会显示。

我觉得接近这样做是通过在Visual Studio中进行拆分视图然后在GridView任务上的“编辑列”按钮,但就像我说我不确定是否可以这样做并且可能必须诉诸于简单编码。

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

使用<TemplateField>代替<ButtonField>

 <asp:TemplateField>
     <ItemTemplate>
         <asp:Button runat="server" Text="View" 
         Visible='<%# Eval("IsEmpty(InvoiceID)") %>' CommandName="ViewInvoice" />
     </ItemTemplate>
 </asp:TemplateField>

在页面中添加IsEmpty(string id)或您的ID所属类型的方法,然后检查它是否为空。

您还可以向Button添加CommandArgument属性,以便您指定它的参数。

答案 1 :(得分:0)

使用占位符...

<asp:Placeholder runat="server" ID="plView" Visible="<%# Convert.ToBoolean(InvoiceID == null) ? false : true %>">
    <asp:ButtonField CommandName="ViewInvoice"  HeaderText=" " ShowHeader="True" Text="View" />
</asp:Placeholder>
相关问题