【问题标题】:hide row on clicking delete link in gridview在gridview中单击删除链接时隐藏行
【发布时间】:2013-04-12 19:26:55
【问题描述】:

我有一个gridview,其中有两个链接按钮。 这里是:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="5" OnRowCommand="gv_RowCommand" 
                        AllowPaging="true" DataKeyNames="ID" CssClass="mGrid"  BackColor="White" BorderColor="Silver" BorderStyle="Double"
                        BorderWidth="1px" CellPadding="4" >

         <RowStyle BackColor="White" Width="150%" ForeColor="#003399" />
            <Columns>
                <asp:TemplateField HeaderText="ID" Visible="false" >
                    <ItemTemplate>
                                    <asp:Label ID="lblID"  Text='<%#Bind("ID") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField  HeaderText="Version">
                    <ItemTemplate>
                                    <asp:Label ID="lblversion" Text='<%#Bind("version") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Image ID">
                    <ItemTemplate>
                                    <asp:Label ID="lblimageid" runat="server" Text='<%#Bind("image_id") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Getty ID">
                    <ItemTemplate>
                                    <asp:Label ID="lblgettyid" runat="server" Text='<%#Bind("getty_id") %>'></asp:Label>
                    </ItemTemplate>
                    <%--<EditItemTemplate>
                                    <asp:TextBox ID="txtgettyid" Text='<%#Bind("getty_id") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>--%>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Pool Letter">
                    <ItemTemplate>
                                    <asp:Label ID="lblpoolletter" runat="server" Text='<%#Bind("pool_letter") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="To Use">
                    <ItemTemplate>
                                    <asp:Label ID="lbltouse" runat="server" Text='<%#Bind("to_use") %>'></asp:Label>
                    </ItemTemplate>
                   <%-- <EditItemTemplate>
                                    <asp:TextBox ID="txttouse" Text='<%#Bind("to_use") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>--%>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Clue">
                    <ItemTemplate>
                                    <asp:Label ID="lblclue" runat="server" Text='<%#Bind("clue") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Range">
                    <ItemTemplate>
                                    <asp:Label ID="lblrange" runat="server" Text='<%#Bind("range") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="last_updated">
                    <ItemTemplate>
                                    <asp:Label ID="lbllastupdated" runat="server" Text='<%#Bind("last_updated") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                                    <asp:Label ID="lblstatus" runat="server" Text='<%#Bind("status") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField  HeaderText="First">
                    <ItemTemplate>
                                    <asp:Label ID="lblfirst" runat="server" Text='<%#Bind("first") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Middle">
                    <ItemTemplate>
                                    <asp:Label ID="lblmiddle" runat="server" Text='<%#Bind("middle") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Last">
                    <ItemTemplate>
                                    <asp:Label ID="lbllast" runat="server" Text='<%#Bind("last") %>'></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>
                 <asp:TemplateField >
                 <ItemStyle Wrap="false" Width="100px" />
                    <ItemTemplate>
                                    <asp:LinkButton ID="lnkbtnedit" Text="Edit" Visible="true" style="color:#006699;"  CommandName="Edit" runat="server"></asp:LinkButton>
                      &nbsp;&nbsp; <asp:LinkButton ID="lnkbtndel" Text="Delete" Visible="true"  style="color:#006699;"  CommandName="Delete" runat="server">  </asp:LinkButton>
                    </ItemTemplate>

                </asp:TemplateField>




            </Columns>
              <PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                        <AlternatingRowStyle BackColor="#CCFFFF" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <HeaderStyle  BackColor="#006699" Font-Bold="True" ForeColor="White"  />
    </asp:GridView>









void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];

            int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

            con = new SqlConnection(conString);
            con.Open();
            int v = version + 1 ;

            string updatedetails = "update tblfameface set status ='Deleted',version=" + v + "  where ID=" + ID + " ";
            SqlCommand cmd = new SqlCommand(updatedetails, con);
            int temp = cmd.ExecuteNonQuery();
            con.Close();

            //int rows = GridView1.CurrentCell.RowIndex;
            //GridView1.Rows.RemoveAt(rows);

            bindDB();

            LinkButton btndel = (LinkButton)GridView1.Rows[row.RowIndex].FindControl("lnkbtndel");
            btndel.Visible = false;

        }

点击编辑链接后,它可以正常工作并且 当我单击删除链接时,我希望隐藏该行。 应该触发哪个事件以及如何处理? 我怎样才能做到这一点??

【问题讨论】:

  • 你想在删除时隐藏行或完全删除行???????
  • @ChetanSanghani-我不想删除记录,我只想把它隐藏在gridview中,但它会存储在数据库中。

标签: asp.net gridview


【解决方案1】:

这是 Aspx:

<Columns>
 <asp:TemplateField HeaderText="Hide"> <ItemTemplate> <asp:LinkButton ID="lnkHide" runat="server" Text="Hide" OnClick="LnkHide_Click" /> </ItemTemplate> </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>

这是背后的代码:

 protected void LnkHide_Click(object sender, EventArgs e)
    {
        GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
        clickedRow.Visible = false;
    }

试试这个代码

【讨论】:

    【解决方案2】:
    private void button1_Click(object sender, EventArgs e)
    {
      int row = dataGridView1.CurrentCell.RowIndex;
      dataGridView1.Rows.RemoveAt(row);
    } 
    

    试试这个代码............

    【讨论】:

    • UR 代码出错。 CurrentCell 和 RemoveAt 不包含定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    相关资源
    最近更新 更多