亲爱的用户,我有一个小问题。我有一个网格视图,包含以下几列:
<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中动态插入一个按钮,其中只有当Data为空时HeaderText=QTA
。你能告诉我什么是正确的方法吗?我想我应该使用TemplateField,但我不知道如何动态地只显示数据字段,或者如果数据为空,如何只显示按钮。重要的按钮和数据必须在同一列中,而不是在两个不同的列中。提前谢谢。
满足您需求的简单解决方案是将Template Field与Label和Button一起使用。尝试以下
<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;
}
}