如何在GridView中隐藏自动生成的删除按钮

时间:2016-02-08 07:58:28

标签: c# asp.net gridview

如何在GridView中隐藏自动生成的删除按钮。无法使删除按钮不可见。删除按钮时取消按钮取消按钮也不会变得不可见。

  

取消按钮也变得不可见

 <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>
                <th scope="col" style="width: 150px;">
                    Company
                </th>
                <th scope="col" style="width: 100px;">
                    Company Code
                </th>
                <th scope="col" style="width: 100px;">
                    Address
                </th>
            </tr>
            <tr>
                <td colspan="99" align = "center">
                    No records found for the search criteria.
                </td>
            </tr>
        </table>
    </EmptyDataTemplate>
        </asp:GridView>



 if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // Hide the edit button when some condition is true
                    // for example, the row contains a certain property
                     if (Loggedinuser != "kernel")
                {


                   e.Row.Cells[0].Controls[2].Visible = false;


                }


                }

1 个答案:

答案 0 :(得分:1)

在GridView中隐藏或显示特定列有两个选项

选项1:使用单元格索引

 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
                    }
                 }        
         }

选项2:通过GridView行控件集合循环

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;
                      }
            }        
    }