0

I want to hide my asp:BoundField with css however when I add visible=false i cant access the values. So I tried to hide it with css howver css is not allowed in BoundFiled so I tried adding a Updatepanel, a div etc but they are not allowed. How can I hide My BoundField with css.

Code I want to hide with css

                <asp:BoundField DataField = "compras_id" HeaderText="Compras ID" HtmlEncode="false" />
                <asp:BoundField DataField = "numero_contrato" HeaderText = "Numero contrato" HtmlEncode="false" />
                <asp:BoundField DataField="marca" HeaderText="Marca" HtmlEncode="false"  />
                <asp:BoundField DataField="designacion" HeaderText="Designacion"  HtmlEncode="false"  />
                <asp:BoundField DataField="tipo" HeaderText="Tipo"  HtmlEncode="false"  />
                <asp:BoundField DataField="referencia" HeaderText="Referencia"  HtmlEncode="false"  />
                <asp:BoundField DataField="plazo" HeaderText="Plazo"  HtmlEncode="false" />
                <asp:BoundField DataField="nombre_proveedor" HeaderText="Proveedor"  HtmlEncode="false"  />
                <asp:BoundField DataField="cantidad_requerida" HeaderText="Cantidad requerida"  HtmlEncode="false" />
                <asp:BoundField DataField="cantidad_pedida" HeaderText="Cantidad pedida"  HtmlEncode="false"  />

My css

.myHide
{
    display:none;
}

Here is why I need to hide the BoundFields this is my front-end

<asp:GridView ID="gvPrimaryGrid" runat="server" AutoGenerateColumns="false" ShowFooter="true" onrowdatabound="gvPrimaryGrid_RowDataBound"
                ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging" DataKeyNames="compras_id"
                 OnRowCommand="gvPrimaryGrid_RowCommand"  OnRowEditing="gvPrimaryGrid_RowEditing" OnRowCancelingEdit="gvPrimaryGrid_RowCancelingEdit"
                OnRowUpdating="gvPrimaryGrid_RowUpdating"  CellPadding="3" AllowUserToResizeColumns="True" PageSize="5" AllowSorting="true" onsorting="gvPrimaryGrid_Sorting"  GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
                <PagerSettings  Mode="Numeric"  />         
                <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                       <%--<asp:CheckBox ID="CheckBox1" AutoPostBack="true" EnableViewState="true"  runat="server" onclick="Check_Click(this)" OnCheckedChanged="chkRow_CheckChanged" />--%>
                       <asp:CheckBox ID="chkRow" AutoPostBack="true"  runat="server" OnCheckedChanged="chkRow_CheckChanged" />
                           <%--<asp:CheckBox ID="chkRow" runat="server" />--%>
                        <asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
                        <asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
                        <asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
                    </FooterTemplate>
                </asp:TemplateField>   

                    <asp:BoundField DataField = "compras_id" HeaderText="Compras ID" HtmlEncode="false" />
                    <asp:BoundField DataField = "numero_contrato" HeaderText = "Numero contrato" HtmlEncode="false" />
                    <asp:BoundField DataField="marca" HeaderText="Marca" HtmlEncode="false"  />
                    <asp:BoundField DataField="designacion" HeaderText="Designacion"  HtmlEncode="false"  />
                    <asp:BoundField DataField="tipo" HeaderText="Tipo"  HtmlEncode="false"  />
                    <asp:BoundField DataField="referencia" HeaderText="Referencia"  HtmlEncode="false"  />
                    <asp:BoundField DataField="plazo" HeaderText="Plazo"  HtmlEncode="false" />
                    <asp:BoundField DataField="nombre_proveedor" HeaderText="Proveedor"  HtmlEncode="false"  />
                    <asp:BoundField DataField="cantidad_requerida" HeaderText="Cantidad requerida"  HtmlEncode="false" />
                    <asp:BoundField DataField="cantidad_pedida" HeaderText="Cantidad pedida"  HtmlEncode="false"  />

                 <asp:TemplateField HeaderText="Compras ID" SortExpression="compras_id" Visible="false">
                    <ItemTemplate>                       
                        <asp:Label DataField="compras_id" HtmlEncode="false"  ID="lblPurschaseID" CssClass="gridTextbox" Text='<%# Eval("compras_id") %>' runat="server" />
                    </ItemTemplate>
                    <%--<EditItemTemplate>
                        <asp:TextBox CssClass="gridTextbox" ID="txtType" Text='<%# Eval("tipo") %>' runat="server" />
                    </EditItemTemplate>--%>
                    <FooterTemplate>
                        <asp:TextBox  CssClass="gridTextbox" ID="txtPurschaseIDFooter" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>    
                  <asp:TemplateField HeaderText="Numero contrato" >
                    <ItemTemplate>                       
                        <asp:Label DataField="numero_contrato"  HtmlEncode="false"  ID="lblContractNumber" CssClass="gridTextbox" Text='<%# Eval("numero_contrato") %>' runat="server" />
                    </ItemTemplate>
                    <EditItemTemplate>   
                <asp:DropDownList  ID="drGridContractID" Runat="server"  AutoPostBack="false"  OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
                          <%-- <asp:TextBox CssClass="gridTextbox" ID="txtSupplier" Text='<%# Eval("nombre_proveedor") %>' runat="server" />--%>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox  CssClass="gridTextbox" ID="txtContractNumberFooter" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>    
                <asp:TemplateField HeaderText="Marca" SortExpression="marca">
                    <ItemTemplate>                       
                        <asp:Label DataField="Marca"  HtmlEncode="false"  ID="lblMarca" CssClass="gridTextbox" Text='<%# Eval("marca") %>' runat="server" />
                    </ItemTemplate>
                     <%-- <EditItemTemplate>
                        <asp:TextBox CssClass="gridTextbox" ID="txtBrand" Text='<%# Eval("marca") %>' runat="server" />
                    </EditItemTemplate>--%>
                    <FooterTemplate>
                        <asp:TextBox  CssClass="gridTextbox" ID="txtCountryFooter" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>

This is my backend

private void GetSelectedRows()
    {
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        if (ViewState["SelectedRows"] != null)
        {
            dt = (DataTable)ViewState["SelectedRows"];
        }
        else
        {
            dt = CreateDataTable();
        }
            //CheckBox chkAll = (CheckBox)gvPrimaryGrid.HeaderRow.Cells[0].FindControl("chkAll");
        for (int i = 0; i < gvPrimaryGrid.Rows.Count; i++)
        {
           /* if (chkAll.Checked)
            {
                dt = AddRow(gvPrimaryGrid.Rows[i], dt);
            }*/
            //else
            //{
                CheckBox chk = ((CheckBox)gvPrimaryGrid.Rows[i].Cells[0].FindControl("chkRow"));
                if (chk.Checked)
                {
                    dt2 = AddRow(gvPrimaryGrid.Rows[i], dt);
                }
                else

                {
                    dt2 = RemoveRow(gvPrimaryGrid.Rows[i], dt);
                }
           // }
        }
        ViewState["SelectedRows"] = dt2;
    }


    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
            dt.Columns.Add("compras_id");
            dt.Columns.Add("numero_contrato");
            dt.Columns.Add("marca");
            dt.Columns.Add("designacion");
            dt.Columns.Add("tipo");
            dt.Columns.Add("referencia");
            dt.Columns.Add("plazo");
            dt.Columns.Add("nombre_proveedor");
            //dt.Columns.Add("cantidad_requerida");
            dt.Columns.Add("cantidad_pedida");
            dt.Columns.Add("cantidad_entregada");
            dt.AcceptChanges();


        return dt;
    }

    private DataTable AddRow(GridViewRow gvRow, DataTable dt)
    {

        DataRow[] dr = dt.Select("compras_id = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length <= 0)
        {
            int i = 1;
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["compras_id"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["numero_contrato"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["marca"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["designacion"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["tipo"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["referencia"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["plazo"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["nombre_proveedor"] = gvRow.Cells[i++].Text;
            //dt.Rows[dt.Rows.Count - 1]["cantidad_requerida"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["cantidad_pedida"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["cantidad_entregada"] = gvRow.Cells[i++].Text;
            dt.AcceptChanges();
        }
        return dt;
    }

    private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
    {
        DataRow[] dr = dt.Select("compras_id = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length > 0)
        {
            dt.Rows.Remove(dr[0]);
            dt.AcceptChanges();
        }
        return dt;
    }

    private void BindSecondaryGrid()
    {
        DataTable dt = (DataTable)ViewState["SelectedRows"];
        gvSecondaryGrid.DataSource = dt;
        gvSecondaryGrid.DataBind();
    }

    protected void chkRow_CheckChanged(object sender, EventArgs e)
    {
        GetSelectedRows();
        SetData();
        BindSecondaryGrid();
    }

    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gvPrimaryGrid.HeaderRow.Cells[0].FindControl("chkAll");
        //chkAll.Checked = true;
        if (ViewState["SelectedRows"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRows"];
            for (int i = 0; i < gvPrimaryGrid.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvPrimaryGrid.Rows[i].Cells[0].FindControl("chkRow");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("compras_id = '" + gvPrimaryGrid.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0;
                    //if (!chk.Checked)
                    //{
                    //    chkAll.Checked = false;
                    //}
                }
            }
        }
    }

As you can see I a using a Session to store the values from the table I have create a table with two columns of the same name for editing and too create a link between the table and a new table were the user can add new rows by checking the checkbox of the first table.

J.C
  • 632
  • 1
  • 10
  • 41

1 Answers1

0

i would use a templatefield rather than a boundfield..

And don't forget, when you set visibility to false on webcontrols, they are not rendered to the response

<asp:templatefield>
   <itemtemplate>
      <span class='myhide'><%# eval("columnname") %></span>
   </itemtemplate>
</asp:templatefield>

Additionally, a repeater will give you precise control over the rendered HTML which may give you better results with what you're trying to do.

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
  • Unfortunately then my code will stop working as I use the databoud in My code I have two table the users clicks on a checkbox to add new the rows to a new table. – J.C Mar 18 '19 at 01:29
  • 1
    there shouldn't be any issues from going from a boundfield to a templatefield, i use them interchangeably. 4guysfromrolla created a series of articles on the datagrid that may clear up any confusion you are having. – Stephen Wrighton Mar 18 '19 at 01:46
  • I will add my code so you can see why it is a problem for me to switch from a boundfield to a templatefield. – J.C Mar 18 '19 at 01:50
  • How can I go from a boundfield to a templatefield with the code I have. – J.C Mar 18 '19 at 09:24
  • Here is my new question https://stackoverflow.com/questions/55246660/when-adding-or-removing-rows-with-templatefield-in-asp-net-the-datatable-is-empt – J.C Mar 20 '19 at 17:16