Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:29:44+00:00 2026-06-01T11:29:44+00:00

I have a gridview that contains value of every rows read from a text

  • 0

I have a gridview that contains value of every rows read from a text file. What I want to do is, edit and delete the selected row on the gridview. I already have the code to show every row on the textfile:

<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>

On the Code Behind:

'''''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()

What I am going to ask is, how do I get the index of selected row return and fill the value to the textbox then update it to the textfile? Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:29:45+00:00Added an answer on June 1, 2026 at 11:29 am

    One suggestion first: a file normally is not the best storage. You might consider to use a dbms instead.

    If you want to edit/delete lines in a file, probably the easiest is to create all lines again.
    You can store the index for example in a 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()
    

    Then you can edit/delete them in the following way:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple gridview that contains a label in one of the rows.
I have a gridview, that shows record according to the selected value in the
I have a gridview that contains user info and one of the columns is
I have a GridView A that have many columns, one of those contains price
I have a GridView. My GridView has a column that is contains an Options
I have ModalPopupExtender that contains a UserControl that basically consists of a GridView and
I have a form that contains a GridView control which is databound to an
I have a user control that contains a GridView. The GridView has both a
I have a datalist that contains a gridview inside its itemtemplate. on the item-data_bound
Basically I have a webcontrol that contains a gridview with an export button. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.