Hi kai,
Refer the below code.
VB.Net
Protected Sub btnExportCSV_Click(ByVal sender As Object, ByVal e As EventArgs)
GridView1.AllowPaging = False
GridView1.DataBind()
Dim sb As New StringBuilder()
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
sb.Append(GridView1.Columns(k).HeaderText + ","c)
Next
'append new line
sb.Append(vbCr & vbLf)
For i As Integer = 0 To GridView1.Rows.Count - 1
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
Next
'append new line
sb.Append(vbCr & vbLf)
Next
' Below line of code save the file in folder path you specified.
Dim folderPath As String = "C:\webise\GridViewExport.csv"
File.WriteAllText(folderPath, sb.ToString())
GridView1.AllowPaging = True
GridView1.DataBind()
End Sub
C#
protected void btnExportCSV_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
//append new line
sb.Append("\r\n");
}
// Below line of code save the file in folder path you specified.
string folderPath = @"C:\webise\";
File.WriteAllText(folderPath + "GridViewExport.csv", sb.ToString());
GridView1.AllowPaging = true;
GridView1.DataBind();
}