Hi guys.. i can bind csv file to Gridview but how can i update it?
Below is my code to Bind csv file to Gridview.
HTML
<asp:GridView ID="GridView" runat="server" DataKeyNames="postcode">
</asp:GridView>
CODE BEHIND
'Upload and save the file
Dim csvPath As String = Server.MapPath("~/csv/") + "location.csv"
'Create a DataTable.
Dim DT As New DataTable()
DT.Columns.Add("Postcode")
DT.Columns.Add("City")
DT.Columns.Add("State")
'DT.Columns.AddRange(New DataColumn(2) {New DataColumn("POSTCODE", GetType(Integer)), New DataColumn("CITY", GetType(String)), New DataColumn("STATE", GetType(String))})
'Read the contents of CSV file.
Dim csvData As String = File.ReadAllText(csvPath)
'Execute a loop over the rows.
For Each row As String In csvData.Split(ControlChars.Lf)
If Not String.IsNullOrEmpty(row) Then
DT.Rows.Add()
Dim i As Integer = 0
'Execute a loop over the columns.
For Each cell As String In row.Split(","c)
DT.Rows(DT.Rows.Count - 1)(i) = cell
i += 1
Next
End If
Next
'Bind the DataTable.
Me.GridView.DataSource = DT
Me.GridView.DataBind()
i can bind csv file to Gridview but how can i update it?
Are you trying to edit the the csv data via this gridview?
If so, i would suggest to add a linkbutton to this gridview and then update the value in the datasource of this gridview(DT in this case) in onrowcommand event, and then export to a csv file.
But i think this can be really complex, i would suggest to store your data in db and then read to gridview, this would make work with data much easier, and you can
export the gridview data to csv file then.
Anyway, the whole process depends on your requirement.
You can refer to below code about how to get specific row value:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindCSV()
End If
End Sub
Protected Sub GridView_RowCommand(sender As Object, e As GridViewCommandEventArgs)
Dim gvr As GridViewRow = CType(((CType(e.CommandSource, LinkButton)).NamingContainer), GridViewRow)
'Dim RowIndex As Integer = gvr.RowIndex
Response.Write(gvr.Cells(1).Text + "," + gvr.Cells(2).Text + "," + gvr.Cells(3).Text)
End Sub
Protected Sub BindCSV()
'Upload and save the file
Dim csvPath As String = Server.MapPath("~/files/") + "location.csv"
'Create a DataTable.
Dim DT As New DataTable()
DT.Columns.Add("Postcode")
DT.Columns.Add("City")
DT.Columns.Add("State")
'DT.Columns.AddRange(New DataColumn(2) {New DataColumn("POSTCODE", GetType(Integer)), New DataColumn("CITY", GetType(String)), New DataColumn("STATE", GetType(String))})
'Read the contents of CSV file.
Dim csvData As String = File.ReadAllText(csvPath)
'Execute a loop over the rows.
For Each row As String In csvData.Split(ControlChars.Lf)
If Not String.IsNullOrEmpty(row) Then
DT.Rows.Add()
Dim i As Integer = 0
'Execute a loop over the columns.
For Each cell As String In row.Split(","c)
DT.Rows(DT.Rows.Count - 1)(i) = cell
i += 1
Next
End If
Next
'Bind the DataTable.
Me.GridView.DataSource = DT
Me.GridView.DataBind()
End Sub
Or if i misunderstood the requirement, please clarify. Thanks!
Member
103 Points
793 Posts
How can update existing data in Gridview (csv file)
Jun 12, 2020 03:37 AM|kengkit|LINK
Hi guys.. i can bind csv file to Gridview but how can i update it?
Below is my code to Bind csv file to Gridview.
HTML
<asp:GridView ID="GridView" runat="server" DataKeyNames="postcode">
</asp:GridView>
CODE BEHIND
Contributor
3140 Points
983 Posts
Re: How can update existing data in Gridview (csv file)
Jun 12, 2020 08:39 AM|Yang Shen|LINK
Hi kengkit,
Are you trying to edit the the csv data via this gridview?
If so, i would suggest to add a linkbutton to this gridview and then update the value in the datasource of this gridview(DT in this case) in onrowcommand event, and then export to a csv file.
But i think this can be really complex, i would suggest to store your data in db and then read to gridview, this would make work with data much easier, and you can export the gridview data to csv file then.
Anyway, the whole process depends on your requirement.
You can refer to below code about how to get specific row value:
aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView" runat="server" DataKeyNames="postcode" OnRowCommand="GridView_RowCommand"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="">Edit</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
vb:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then BindCSV() End If End Sub Protected Sub GridView_RowCommand(sender As Object, e As GridViewCommandEventArgs) Dim gvr As GridViewRow = CType(((CType(e.CommandSource, LinkButton)).NamingContainer), GridViewRow) 'Dim RowIndex As Integer = gvr.RowIndex Response.Write(gvr.Cells(1).Text + "," + gvr.Cells(2).Text + "," + gvr.Cells(3).Text) End Sub Protected Sub BindCSV() 'Upload and save the file Dim csvPath As String = Server.MapPath("~/files/") + "location.csv" 'Create a DataTable. Dim DT As New DataTable() DT.Columns.Add("Postcode") DT.Columns.Add("City") DT.Columns.Add("State") 'DT.Columns.AddRange(New DataColumn(2) {New DataColumn("POSTCODE", GetType(Integer)), New DataColumn("CITY", GetType(String)), New DataColumn("STATE", GetType(String))}) 'Read the contents of CSV file. Dim csvData As String = File.ReadAllText(csvPath) 'Execute a loop over the rows. For Each row As String In csvData.Split(ControlChars.Lf) If Not String.IsNullOrEmpty(row) Then DT.Rows.Add() Dim i As Integer = 0 'Execute a loop over the columns. For Each cell As String In row.Split(","c) DT.Rows(DT.Rows.Count - 1)(i) = cell i += 1 Next End If Next 'Bind the DataTable. Me.GridView.DataSource = DT Me.GridView.DataBind() End Sub
Or if i misunderstood the requirement, please clarify. Thanks!
Best Regard,
Yang Shen
Member
103 Points
793 Posts
Re: How can update existing data in Gridview (csv file)
Jun 14, 2020 08:23 AM|kengkit|LINK
Ya. seem like the best way is convert all my csv data to database instead of add new/ update from the existing csv file. tqtq