convert datagrid data to csv file?
hi,
first i bind a data into datagrid, this data is displayed in .csv file how to do this? i am try but if long text is displayed in datagrid that time in csv file it take 2 or 3 lines but my aim is if user enter long text that time also it display in single line.
and if i open this csv file in wordpad that time it display comma to last column also. my aim is comma is not displayed for last column.
my code:
Private Function ToCSV(ByVal dataTable As DataTable) As String
Dim sb As New StringBuilder()
If dataTable.Columns.Count <> 0 Then
For Each column As DataColumn In dataTable.Columns
sb.Append("," & column.ColumnName)
Next
sb.Remove(0, 1)
sb.Append(vbCr & vbLf)
For Each row As DataRow In dataTable.Rows
For Each column As DataColumn In dataTable.Columns
sb.Append(row(column).ToString() + ","c)
Next
sb.Append(vbCrLf)
Next
End If
Return sb.ToString()
End Function
and in button click
'// here i bind datagrid
and then
Response.ContentType = "Application/x-msexcel"
Response.AddHeader("content-disposition", "attachment;filename=Reports.csv")
Response.Write(ToCSV(tdata))
Response.[End]()
please help me
Thanks.