1

我有一个gridview,其中包含从文本文件中读取的每一行的值。我想要做的是,编辑和删除gridview上的选定行。我已经有了显示文本文件每一行的代码:

<asp:GridView ID="GVAnnouncement" runat="server" 
  AutoGenerateColumns="True" EmptyDataText="- No file saved -">
  <Columns>
    <asp:TemplateField HeaderText="No.">
      <ItemTemplate>
        <%# Container.DisplayIndex + 1%>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

关于背后的代码:

'''''Read every row then show it on gridview'''''

' Declarations  
Dim objStreamReader As New StreamReader(Server.MapPath("Announcement.txt"))
Dim arrText As New ArrayList

' Loop through the file and add each line to the ArrayList  
Do While objStreamReader.Peek() >= 0
    arrText.Add(objStreamReader.ReadLine)
Loop

' Close the reader  
objStreamReader.Close()

' Bind the results to the GridView  
GVAnnouncement.DataSource = arrText
GVAnnouncement.DataBind()

我要问的是,如何获取所选行返回的索引并将值填充到文本框然后将其更新到文本文件?非常感谢你。

4

2 回答 2

1

您可以使用 void RowDeleted。我会在标识行的 Datakeys 中存储一些东西。您可以keys在其中使用它RowDeleted并做任何您想做的事情。像这样:

ASPX

<asp:GridView ID="GVAnnouncement" DataKeyNames="ID" runat="server" 
onrowdeleted="GVAnnouncement_RowDeleted" ...

CS

Sub GVAnnouncement_RowDeleted(ByVal sender As Object, ByVal e As GridViewDeletedEventArgs)
   e.Keys("ID")
End Sub

在文件中找到ID并删除该行然后再次绑定网格

于 2012-04-07T09:46:37.857 回答
1

一个建议首先:文件通常不是最好的存储。您可能会考虑改用dbms

如果要编辑/删除文件中的行,可能最简单的方法是再次创建所有行。例如,您可以将索引存储在HiddenField.

<ItemTemplate>
    <asp:HiddenField runat="server" ID="HiddenIndex" Value="<%# Container.DataItem.Index %>" />
    <asp:Label ID="LblAnnouncement" Text="<%# Container.DataItem.Announcement %>" runat="server"></asp:Label>
</ItemTemplate>

Dim allAnnouncements = IO.File.ReadAllLines(Server.MapPath("Announcement.txt"))
Dim source = allAnnouncements.Select(Function(l, index) New With {.Index = index, .Announcement = l}).ToList

' Bind the results to the GridView  
GVAnnouncement.DataSource = source 
GVAnnouncement.DataBind()

然后您可以通过以下方式编辑/删除它们:

Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)  
    Dim grid = DirectCast(sender, GridView)
    Dim index = Int32.Parse(DirectCast(grid.Rows(e.RowIndex).FindControl("HiddenIndex"), HiddenField).Value)
    Dim oldAnnamouncement = e.OldValues(0).ToString
    Dim newAnnouncement = e.NewValues(0).ToString
    If Not oldAnnamouncement.Equals(newAnnouncement) Then
        Dim allAnnouncements = IO.File.ReadAllLines(Server.MapPath("Announcement.txt"))
        allAnnouncements(index) = newAnnouncement
        IO.File.WriteAllLines(Server.MapPath("Announcement.txt"), allAnnouncements)
    End If

    'Reset the edit index.
    GridView1.EditIndex = -1

    'Bind data to the GridView control.
    BindData()
End Sub



Protected Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
    Dim grid = DirectCast(sender, GridView)
    Dim index = Int32.Parse(DirectCast(grid.Rows(e.RowIndex).FindControl("HiddenIndex"), HiddenField).Value)

    Dim allAnnouncements = IO.File.ReadAllLines(Server.MapPath("Announcement.txt"))
    IO.File.WriteAllLines(Server.MapPath("Announcement.txt"), allAnnouncements.Where(Function(l, i) index <> i))

    'Bind data to the GridView control.
    BindData()
End Sub
于 2012-04-07T09:46:51.667 回答