Datagridview to notepad Exported textfile need the delimited value

Posted by Agopi.net under C# on 6/4/2012 | Points: 10 | Views : 7423 | Status : [Member] | Replies : 4
Hi,

I want to export the data to notepad (.txt file). I got the code for export the datagridview to notepad. But i need to the exported .txt file as below method (, " ) delimited.

"1","Inthiyaaz","300000"
"2","Imroz","13000"
"3","Afroz Basha","25000"

Below is the link for Export the gridview to .txt file.

http://www.dotnetfunda.com/articles/article393-exporting-data-to-txt-file-using-csharp.aspx

can anybody let me know this details.

By
Gopi A




Responses

Posted by: T.Saravanan on: 6/4/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

try the below code...

// Here I am using DataTable instead of GridView.


public void WriteTextFile(DataTable dtData, string FilePath)
{
using (StreamWriter sw = new StreamWriter(FilePath, false))
{
for (int x = 0; x < dtData.Columns.Count; x++)
{
sw.Write(dtData.Columns[x].ColumnName + ",");
}
sw.WriteLine();

foreach (DataRow row in dtData.Rows)
{
for (int n = 0; n < dtData.Columns.Count; n++)
{
sw.Write(row[n].ToString() + ",");
}
sw.WriteLine();
}
sw.Close();
}
}


Thanks,
T.Saravanan

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Saravanan,

Could you please let me know what kind of data to pass here. Can you please send me the full code on this.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

I am struggled and get confusion that what kind of data should pass in these kind of functions. could you please explain your code (please provide the full code)

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 6/10/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Singravel,

Thanks for your help. I have changed the below line as per your instruction. But I hope this is not a correct method even we got the answer. We need to placed the delimiter after read the values from gridview by cell wise.

sw.WriteLine("\""+ dataGridView1.Rows[i].Cells[0].Value.ToString() + "\",\"" + dataGridView1.Rows[i].Cells[1].Value.ToString()+ "\"" );



Also i need explanation on Mr.Saravanan Code.

What kind of data we should pass.
Where we should pass (from any button control)


Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response