在GridView上编辑和删除文本文件的行(VB .Net)

时间:2012-04-07 09:14:47

标签: asp.net vb.net gridview text-files

我有一个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()

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

2 个答案:

答案 0 :(得分: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并删除该行,然后再次绑定网格

答案 1 :(得分: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