Step 1: Write this code on Button click event
if
(GridLoginStatus.Rows.Count != 0)
{
string
strFilePath =
ConfigurationManager.AppSettings["CSVFilePath"];
CreateCSVFile(dt, strFilePath);
lblErrorMsg.Text = "Report Saved";
}
else
{
lblErrorMsg.Text = "No Data Captured";
}
Step 2: Add a function on the form
public void CreateCSVFile(DataTable
dt, string strFilePath)
{
// Create the
CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(strFilePath, false);
// First we
will write the headers.
int
iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i <
iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write
all the rows.
foreach
(DataRow dr in
dt.Rows)
{
for
(int i = 0; i < iColCount; i++)
{
if
(!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if
(i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
Thanks for this post.
ReplyDeleteWelcome..
Deletegood source code with using c# using asp.net
ReplyDeletethanks
helpfull provided code .