Dear users I have a little issue. I have a gridview with several columns as follows:
<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>
I would like to dynamically insert a button inside the BoundField where HeaderText=QTA
only when the Data is empty. Could you tell me what is the correct way? I suppose I should use a TemplateField but I don't know how to dynamically show only the data field, or how show only the button if the data is empty.
Important! The button and the data must be in the same column, not in 2 different columns.
Thank you in advance.
The simple solutions for your requirement is to use Template Field with Label and Button. Try out following
<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>
On codeBehind:
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;
}
}