I have looked at the all the provided solutions here but none seems to help with my specific problem.
我已经查看了所有提供的解决方案,但似乎没有一个解决我的具体问题。
I am creating rows dynamically in GridView which is working great.
我在GridView中动态创建行,效果很好。
However, we would like to delete the last row added.
但是,我们想删除添加的最后一行。
My code is deleting the default row which is the first row.
我的代码是删除第一行的默认行。
Any ideas what I need to modify to ensure that only the last added row gets deleted when user clicks the Delete button?
我需要修改哪些想法以确保在用户单击“删除”按钮时仅删除最后添加的行?
//Markup
<asp:gridview ID="Gridview1" RowStyle-Wrap="false" gridlines="None" CssClass="responsiveTable1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting" OnRowCreated="Gridview1_RowCreated">
<Columns>
<asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Name">
<headerstyle horizontalalign="Left" />
<ItemTemplate>
<asp:TextBox ID="txtsourcename" placeholder="Name...(e.g, Jane Doe)" runat="server" style="width:375px;" AutoPostBack="true" class="form-control textClass" OnTextChanged="txtsourcename_TextChanged"></asp:TextBox><br />
<asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="Grid1CheckChanged" /><span style="color:#ff0000">*Check this box if N/A</span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtsourceaddress" placeholder="Address..." runat="server" style="width:375px;" class="form-control textClass"></asp:TextBox><br /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add"
onclick="ButtonAdd_Click" CssClass="grvAddButton" /><br /><br /><br>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="sourceDelete" runat="server" Text="Delete" CommandName="Delete"
CssClass="grvDelButton" OnClientClick="return confirm('Are you sure you want to remove this row?')" /> <br /><br /><br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
//VB
// VB
Protected Sub Gridview1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim lb As Button = CType(e.Row.FindControl("sourceDelete"), Button)
If lb IsNot Nothing Then
If dt.Rows.Count > 1 Then
If e.Row.RowIndex = dt.Rows.Count - 1 Then
lb.Visible = False
End If
Else
lb.Visible = False
End If
End If
End If
End Sub
Protected Sub sourceDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim lb As Button = CType(sender, Button)
Dim gvRow As GridViewRow = CType(lb.NamingContainer, GridViewRow)
Dim rowID As Integer = gvRow.RowIndex
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 1 Then
If gvRow.RowIndex < dt.Rows.Count - 1 Then
dt.Rows.Remove(dt.Rows(rowID))
ResetRowID(dt)
End If
End If
ViewState("CurrentTable") = dt
Gridview1.DataSource = dt
Gridview1.DataBind()
End If
SetPreviousData()
End Sub
Private Sub ResetRowID(ByVal dt As DataTable)
Dim rowNumber As Integer = 1
If dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
row(0) = rowNumber
rowNumber += 1
Next
End If
End Sub
1 个解决方案
#1
0
It seems that below line deletes a specific row
似乎下面的行删除了一个特定的行
dt.Rows.Remove(dt.Rows(rowID))
try remove the count of dt - 1
尝试删除dt - 1的计数
dt.Rows.Remove(dt.Rows(dt.Rows.count() -1))
#1
0
It seems that below line deletes a specific row
似乎下面的行删除了一个特定的行
dt.Rows.Remove(dt.Rows(rowID))
try remove the count of dt - 1
尝试删除dt - 1的计数
dt.Rows.Remove(dt.Rows(dt.Rows.count() -1))