troubleshooting Question

Hide Delete button until a row is added by clicking Add button, works partially

Avatar of sammySeltzer
sammySeltzerFlag for United States of America asked on
* DataGridViewASP.NETVisual Basic.NET
6 Comments1 Solution81 ViewsLast Modified:
Greetings mates.

Hope the title of this thread makes sense.

When our users load our current app, it displays among other things, an Add button used to dynamically create additional rows as needed and a Delete button so users can delete any additional rows they added but no longer needs.

This part works great.

However, our boss wanted only show the Add button and only expose the Delete button after a new row is added.

The code below does just that.

<asp:GridView ID="grvGift" AutoGenerateColumns="false" runat="server"
    onrowdatabound="grvGift_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Income">
            <ItemTemplate>
                <asp:TextBox ID="txtGiftname" placeholder="Name..." runat="server" Style="width: 250px;"
                    class="form-control"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="&nbsp;">
            <ItemTemplate>
                <asp:Button ID="ButtonAdd1" runat="server" Text="Add" OnClick="ButtonAdd1_Click" CssClass="grvAddButton" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="True">
            <ControlStyle  CssClass="grvDelButton" />
        </asp:CommandField>
    </Columns>
</asp:GridView><br />
<asp:GridView ID="grvOrg" AutoGenerateColumns="false" runat="server"
    onrowdatabound="grvOrg_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Income">
            <ItemTemplate>
                <asp:TextBox ID="txtorgname" placeholder="Name..." runat="server" Style="width: 250px;"
                    class="form-control"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="&nbsp;">
            <ItemTemplate>
                <asp:Button ID="ButtonAdd2" runat="server" Text="Add" OnClick="ButtonAdd2_Click" CssClass="grvAddButton" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="True">
            <ControlStyle  CssClass="grvDelButton" />
        </asp:CommandField>
    </Columns>
</asp:GridView><br />
<asp:GridView ID="grvCredit" AutoGenerateColumns="false" runat="server"
    onrowdatabound="grvCredit_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Income">
            <ItemTemplate>
                <asp:TextBox ID="txtCreditname" placeholder="Name..." runat="server" Style="width: 250px;"
                    class="form-control"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="&nbsp;">
            <ItemTemplate>
                <asp:Button ID="ButtonAdd3" runat="server" Text="Add" OnClick="ButtonAdd3_Click" CssClass="grvAddButton" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="True">
            <ControlStyle  CssClass="grvDelButton" />
        </asp:CommandField>
    </Columns>
</asp:GridView>



    Protected Sub grvGift_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            If e.Row.RowIndex = -1 Then
                DirectCast(DirectCast(e.Row.Cells(5), System.Web.UI.WebControls.DataControlFieldCell).ContainingField, System.Web.UI.WebControls.CommandField).ShowDeleteButton = False
            End If
        Else
            DirectCast(DirectCast(e.Row.Cells(5), System.Web.UI.WebControls.DataControlFieldCell).ContainingField, System.Web.UI.WebControls.CommandField).ShowDeleteButton = True
        End If
    End Sub

    Protected Sub grvOrg_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            If e.Row.RowIndex = -1 Then
                DirectCast(DirectCast(e.Row.Cells(5), System.Web.UI.WebControls.DataControlFieldCell).ContainingField, System.Web.UI.WebControls.CommandField).ShowDeleteButton = False
            End If
        Else
            DirectCast(DirectCast(e.Row.Cells(5), System.Web.UI.WebControls.DataControlFieldCell).ContainingField, System.Web.UI.WebControls.CommandField).ShowDeleteButton = True
        End If
    End Sub

    Protected Sub grvCredit_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            If e.Row.RowIndex = -1 Then
                DirectCast(DirectCast(e.Row.Cells(5), System.Web.UI.WebControls.DataControlFieldCell).ContainingField, System.Web.UI.WebControls.CommandField).ShowDeleteButton = False
            End If
        Else
            DirectCast(DirectCast(e.Row.Cells(5), System.Web.UI.WebControls.DataControlFieldCell).ContainingField, System.Web.UI.WebControls.CommandField).ShowDeleteButton = True
        End If
    End Sub

The problem is that when you click the Add button and a new row is added successfully, rather than expose the Delete button for that particular GridView, all the Delete buttons for all three Gridviews are exposed.

It is not how we want it to work.

Any ideas what I need to change on any part of the code below to only expose the Delete button for only the GridView control ID that added the new row?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros