0

This gridview shows values of table "productos" currently is displaying a button in all of the rows but i need to show a button only in the rows where the stock(Existencias) is higher than 0

<div>    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"ConnectionString="<%$ ConnectionStrings:bodegahyhConnectionString %>" SelectCommand="SELECT * FROM [Productos]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="IdProducto" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="IdProducto" HeaderText="IdProducto" InsertVisible="False" ReadOnly="True" SortExpression="IdProducto" />
            <asp:BoundField DataField="IdCiudad" HeaderText="IdCiudad" SortExpression="IdCiudad" />
            <asp:BoundField DataField="IdTamano" HeaderText="IdTamano" SortExpression="IdTamano" />
            <asp:BoundField DataField="IdFragilidad" HeaderText="IdFragilidad" SortExpression="IdFragilidad" />
            <asp:BoundField DataField="IdMarca" HeaderText="IdMarca" SortExpression="IdMarca" />
            <asp:BoundField DataField="IdUbicacion" HeaderText="IdUbicacion" SortExpression="IdUbicacion" />
            <asp:BoundField DataField="IdProveedor" HeaderText="IdProveedor" SortExpression="IdProveedor" />
            <asp:BoundField DataField="NomProducto" HeaderText="NomProducto" SortExpression="NomProducto" />
            <asp:BoundField DataField="Descripcion" HeaderText="Descripcion" SortExpression="Descripcion" />
            <asp:BoundField DataField="Existencias" HeaderText="Existencias" SortExpression="Existencias" />
            <asp:BoundField DataField="PrecioVenta" HeaderText="PrecioVenta" SortExpression="PrecioVenta" />
            <asp:BoundField DataField="PrecioCompra" HeaderText="PrecioCompra" SortExpression="PrecioCompra" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button id="btn_agregar" runat="server" Text="Agregar" Enabled="true" />
            </ItemTemplate>
            </asp:TemplateField>
       </Columns>
    </asp:GridView>
    <br />
    <br />    
</div>
VDWWD
  • 35,079
  • 22
  • 62
  • 79

2 Answers2

0

Try as said below (Code is not tested..)

Use RowDataBound event. The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control.

First add onrowdatabound="CustomersGridView_RowDataBound to your ASP gridview definition as shown below.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="IdProducto" DataSourceID="SqlDataSource1" onrowdatabound="CustomersGridView_RowDataBound">

Next in your code behind file handle the event and show/hide the button as shown below.

 void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {

      //GET THE BUTTON.
      Button button1 = (Button)e.Row.FindControl("btn_agregar");

      //CHECK CONDITION AND SHOW/HIDE ACCORDINGLY.
      if (YOUR CONDITION)
         button1.Visible = true; 
      else
         button1..Visible = false; 

    }
 }
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
0

You can do this easily by checking the column value as an inline if statement that returns a boolean for the Visibility property.

<asp:Button id="btn_agregar" runat="server" Visible='<%# Convert.ToInt32(Eval("IdProducto")) > 0 %>' Text="Agregar" />
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • thank you very much VDWWD it worked perfectly, that thing between <%# %> , is that javascript? (i'm beginner) – Medina Nualart Martin May 01 '17 at 18:17
  • No, that is aspnet code. See here for more info http://stackoverflow.com/questions/5833278/meaning-of-the-various-symbols-in-aspx-page-of-asp-net – VDWWD May 01 '17 at 18:59