krishna mohan February 2016

How to hide the auto generated delete button in GridView

How to hide the auto generated delete button in GridView. unable to make only delete button invisible. while making invisible of delete button cancel button also not getting invisiable.

cancel button is also getting invisible

 <asp:GridView ID="gvCompanies" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager"
            HeaderStyle-CssClass="header" RowStyle-CssClass="rows"
            AllowPaging="True" OnPageIndexChanging="gvCompanies_PageIndexChanging"  AutoGenerateColumns="False" EmptyDataText="No records found" OnRowEditing="gvCompanies_RowEditing" AutoGenerateEditButton="true" OnRowUpdating="gvCompanies_RowUpdating" OnRowCancelingEdit="gvCompanies_RowCancelingEdit" OnRowDeleting="gvCompanies_RowDeleting" AutoGenerateDeleteButton="true" OnRowDataBound="gvCompanies_RowDataBound" >
            <Columns>
             <asp:TemplateField HeaderText="S No.">
              <ItemTemplate>

                <asp:Label ID="lblid" runat="server" Text='<%#Eval ("id")%>'></asp:Label>
                     </ItemTemplate>
                     </asp:TemplateField>
  <asp:TemplateField HeaderText="Company" ItemStyle-ForeColor="black">
<ItemTemplate>
     <a href='services.aspx?CompanyId=<%#Eval("id")%>'>
<asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Company")%>'/>
         </a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# Eval("Company")%>'/>
</EditItemTemplate>
             </Columns>
              <EmptyDataTemplate>
              <table cellspacing="0" rules="all" border="0" style="border-collapse: collapse;">
            <tr style="color: White; background-color: #3AC0F2;">
                <th scope="col" style="width: 150px;">
                    SL No
                </th>
                &l        

Answers


Sruthi Suresh February 2016

There are two options to hide or show a specific column in GridView

Option 1: Using the Cells index

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
          if (e.Row.RowType == DataControlRowType.DataRow)
                {
                if("your Condition")
                  {

                    e.Row.Cells[0].Control[0].Visible = false;//or true
                    }
                 }        
         }

Option 2: Looping through GridView Row Controls collections

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {


            foreach (TableRow row in GridView1.Controls[0].Controls)
            {
             if("your Condition")
                      {
                      row.Cells[0].Control[0].Visible = false;
                      }
            }        
    }

Post Status

Asked in February 2016
Viewed 1,763 times
Voted 13
Answered 1 times

Search




Leave an answer