如何在编辑单击时从gridview隐藏列?

时间:2019-01-03 08:59:39

标签: c# asp.net

“我正在使用asp.net gridview,当用户单击“编辑”按钮时,下拉列表列应该隐藏。我应该怎么做使其隐藏?我已附加以下代码。gridview包含学生数据,gridview将显示根据用户选择的分类数据记录。我曾尝试对OnRowDataBound隐藏该列,但对我不起作用。有什么办法可以隐藏特定的列OnRowEditing事件?

 <asp:GridView ID="GridView1" runat="server"  DataKeyNames="id"  
                    CssClass="table table-bordered table-striped" 
  AutoGenerateColumns="False"
                 HorizontalAlign="Center" CellPadding="4" 
                    EmptyDataText="No Record Found....."    
  onrowcancelingedit="GridView1_RowCancelingEdit" 
                 onrowediting="GridView1_RowEditing"  
                    onrowupdating="GridView1_RowUpdating" 
  ForeColor="#333333" 
                    GridLines="None" 
  OnPageIndexChanging="OnPageIndexChanging" PageSize="15" 
  AllowPaging="true" >

            <PagerSettings Position="Bottom" />                       
                <AlternatingRowStyle BackColor="White" 
  ForeColor="#284775" 
   />
             <Columns>                                         


               <asp:TemplateField>                      
               <ItemTemplate>            
               <asp:CheckBox ID="chkRow" runat="server"/>
               </ItemTemplate>
               </asp:TemplateField>   

                   <asp:TemplateField HeaderText="Sr.No">
                    <ItemTemplate>
                        <asp:Label ID="lblId" runat="server" 
     Text='<%#Container.DataItemIndex+1%>'></asp:Label>
                    </ItemTemplate>
                   </asp:TemplateField>

                        <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                        <asp:Label ID="lblFullName" runat="server" 
    Text='<%#Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>     

                        <asp:TemplateField HeaderText="Ack">
                        <ItemTemplate>


                                 <edititemtemplate>
                                     <asp:DropDownList 
              ID="Drpacknowledgement" AutoPostBack="True"

     onselectedindexchanged="Drpacknowledgement_SelectedIndexChanged" 
   runat="server">                                
                        <asp:ListItem>--Select--</asp:ListItem>
                        <asp:ListItem>Received</asp:ListItem>
                        <asp:ListItem>Reject</asp:ListItem>                               
                        </asp:DropDownList>

                              </edititemtemplate>


                        </ItemTemplate>

                        </asp:TemplateField>

                   <asp:TemplateField HeaderText="Edit/Update">
                    <ItemTemplate>
                        <asp:LinkButton ID="LkB1" runat="server" 
  CommandName="Edit">Edit</asp:LinkButton>                           
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LkB2" runat="server" 
   CommandName="Update">Update</asp:LinkButton>
                        <asp:LinkButton ID="LkB3" runat="server" 
   CommandName="Cancel">Cancel</asp:LinkButton>
                    </EditItemTemplate>                          
                  </asp:TemplateField>

                  </Columns>



             </asp:GridView>  


   Codebehind:
   protected void GridView1_RowEditing(object sender, 
   GridViewEditEventArgs 
   e)
    {
        try
        {
            string Id = 
            GridView1.DataKeys[e.NewEditIndex].Value.ToString();
            GridView1.EditIndex = e.NewEditIndex;
            ViewState["Id"] = Id;
            EditRecord();
        }
        catch (Exception ex)
        {

        }
    }   

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,我认为这可以解决问题

protected void GridView1_DataBound(object sender, EventArgs e)
    {
        if (GridView1.EditIndex > -1)
            GridView1.Columns[5].Visible = false;
        else
            GridView1.Columns[5].Visible = true;
    }

答案 1 :(得分:0)

使用行命令单击按钮。

https://www.codeproject.com/Tips/564619/%2FTips%2F564619%2FExample-of-gridview-rowcommand-on-Button-Click

 <asp:GridView ID="GridView1" runat="server"  DataKeyNames="id"  
                        CssClass="table table-bordered table-striped" 
      AutoGenerateColumns="False"
                     HorizontalAlign="Center" CellPadding="4" 
                        EmptyDataText="No Record Found....."    
      onrowcancelingedit="GridView1_RowCancelingEdit" 
                     onrowediting="GridView1_RowEditing"  
                        onrowupdating="GridView1_RowUpdating" 
      ForeColor="#333333" 
                        GridLines="None" 
      OnPageIndexChanging="OnPageIndexChanging" PageSize="15" 
       OnRowCommand="GridView1_RowCommand"
      AllowPaging="true" >

                <PagerSettings Position="Bottom" />                       
                    <AlternatingRowStyle BackColor="White" 
      ForeColor="#284775" 
       />

         <asp:TemplateField HeaderText="Edit/Update">
                            <ItemTemplate>
                                <asp:LinkButton ID="LkB1" runat="server" CommandName="Edit"
          CommandName="Edit">Edit</asp:LinkButton>                           
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="LkB2" runat="server" 
           CommandName="Update">Update</asp:LinkButton>
                                <asp:LinkButton ID="LkB3" runat="server" 
           CommandName="Cancel">Cancel</asp:LinkButton>
                            </EditItemTemplate>                          
                          </asp:TemplateField>


     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
      if (e.CommandName == "Edit")
            {
    //do your stuff here
 GridView1.Columns[5].Visible = false;
    }

    }