0

亲爱的用户我有一个小问题。我有一个包含几列的gridview,如下所示:

<asp:GridView>
        <Columns>
            <asp:CommandField SelectText="+" ShowSelectButton="True" />
            <asp:BoundField ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" DataField="TE_ID_ANALYSIS" HeaderText="TE_ID_ANALYSIS" ReadOnly="True" SortExpression="TE_ID_ANALYSIS"/>
            <asp:BoundField DataField="DATA" HeaderText="DATA" ReadOnly="True" SortExpression="DATA" />
            <asp:BoundField DataField="TE_NDOC" HeaderText="NUMERO" SortExpression="TE_NDOC" />
            <asp:BoundField DataField="RIFERIMENTO" HeaderText="RIFERIMENTO" ReadOnly="True" SortExpression="RIFERIMENTO" ItemStyle-Width="32%" />
            <asp:BoundField DataField="TE_ID_STATUS" HeaderText="STATUS" ReadOnly="true" />
            <asp:BoundField DataField="MM_QTA" HeaderText="QTA" ReadOnly="true" SortExpression="MM_QTA" />
            <asp:BoundField DataField="MM_IMPORTO" HeaderText="IMPORTO" ReadOnly="true" SortExpression="MM_IMPORTO" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:F}" />          
        </Columns>
</asp:GridView>

我想在 BoundField 内动态插入一个按钮,HeaderText=QTA仅当数据为空时。你能告诉我正确的方法是什么吗?我想我应该使用 TemplateField 但我不知道如何仅动态显示数据字段,或者如果数据为空则如何仅显示按钮。重要的!按钮和数据必须在同一列中,而不是在 2 个不同的列中。先感谢您。

4

1 回答 1

0

满足您要求的简单解决方案是使用带有标签和按钮的模板字段。尝试以下

    <asp:GridView ID="gridview1" AutoGenerateColumns="False" OnRowDataBound="gridview1_RowDataBound">
            <Columns>
                <asp:CommandField SelectText="+" ShowSelectButton="True" />
                <asp:BoundField ItemStyle-CssClass="hiddencol"  HeaderStyle-CssClass="hiddencol" DataField="TE_ID_ANALYSIS" HeaderText="TE_ID_ANALYSIS" ReadOnly="True" SortExpression="TE_ID_ANALYSIS"/>
                <asp:BoundField DataField="DATA" HeaderText="DATA" ReadOnly="True" SortExpression="DATA" />
                <asp:BoundField DataField="TE_NDOC" HeaderText="NUMERO" SortExpression="TE_NDOC" />
                <asp:BoundField DataField="RIFERIMENTO" HeaderText="RIFERIMENTO" ReadOnly="True" SortExpression="RIFERIMENTO" ItemStyle-Width="32%" />
                <asp:BoundField DataField="TE_ID_STATUS" HeaderText="STATUS" ReadOnly="true" />
<asp:TemplateField HeaderText="QTA">
                            <ItemTemplate>
                                <asp:Label ID="lblQTA" runat="server" Text='<%# Eval("MM_QTA")%>'></asp:Label>
<asp:Button ID="btnQTA" runat="server" Text="Click" Visible="false" />
                            </ItemTemplate>
                        </asp:TemplateField>
                <asp:BoundField DataField="MM_IMPORTO" HeaderText="IMPORTO" ReadOnly="true" SortExpression="MM_IMPORTO" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:F}" />          
            </Columns>
    </asp:GridView>

在代码后面:

protected void gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
          Label lblqta = e.Row.FindControl("lblQTA") as Label;
          if(lblqta.Text == "")
            btnQTA.Visible =true;
        }
    } 
于 2016-05-17T08:58:53.910 回答