簡體   English   中英

如何在GridView中隱藏自動生成的刪除按鈕

[英]How to hide the auto generated delete button in 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;


                }


                }

有兩個選項可隱藏或顯示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;
                      }
            }        
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM