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.
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.Then you can edit/delete them in the following way: