Link to home
Start Free TrialLog in
Avatar of A_Kardas
A_Kardas

asked on

C# code to output to TAB delimited file

I have a function that outputs the data i want to a comma separated file, but how can i ajust this to output to a TAB separated file? i was thinking it would be just /t where the "," are.

But that didnt seem to work. An i missing something?

Just for your information, DataGrid is actually a dataGridView.
private void ExportGridToCsv(string file)
{
    StreamWriter writer = new StreamWriter(file);
 
    if (DataGrid.Rows.Count > 0)
    {
        foreach (DataGridViewColumn col in DataGrid.Columns)
        {
            if (col.Index == DataGrid.Columns.Count - 1)
            {
                writer.WriteLine(col.HeaderText);
            }
            else
            {
                writer.Write(string.Concat(col.HeaderText, ","));
            }
        }
 
        foreach (DataGridViewRow row in DataGrid.Rows)
        {
            
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.OwningColumn.Index == DataGrid.Columns.Count - 1)
                    {
                        if (cell.Value != null)
                            writer.WriteLine(cell.Value.ToString());
                        else
                            writer.WriteLine("");
                    }
                    else
                    {
                        if (cell.Value != null)
                            writer.Write(string.Concat(cell.Value.ToString(), ","));
                        else
                            writer.Write(string.Concat("", ","));
                    }
                }
            
        }
    }
 
    writer.Close();
}

Open in new window

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

\\t helps?
ASKER CERTIFIED SOLUTION
Avatar of Toms Edison
Toms Edison
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
single back slash followed by tee (\t)