Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have an application I've been working on that generates a CSV from a datagrid. This is a WinForms application. I need to learn how to rename my column header text from the default table column header texts because it is not understandable by my users.

StreamWriter csvwriter = new StreamWriter(filename);

                    cols = cj96TBRiscDataGridView.Columns.Count;
                    for (int loop = 0; loop < cols; loop++)
                    {
                        csvwriter.Write(cj96TBRiscDataGridView.Columns[loop].HeaderText + ",");
                    }
                    csvwriter.WriteLine();


The above snippet is the code I am currently using to auto-generate the column names. I hadn't thought about it prior to testing it. Thanks the the responses in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jun-14 10:03am    
Why not putting "understandable" header in first place?
Why not generating CSV with corrected header in first place?
And why using CSV at all?
—SA

1 solution

The best you could do is using a Dictionary to map the column headers with the header you want to use in your csv file.

Something like that:
C#
Dictionary<string, string> headerMap = new Dictionary<string, string>();
headerMap.Add("DGVHeaderText", "CSVFileHeaderText");
// etc.


Then you would only have to:
C#
StreamWriter csvwriter = new StreamWriter(filename);
cols = cj96TBRiscDataGridView.Columns.Count;
for (int loop = 0; loop < cols; loop++)
{
   csvwriter.Write(headerMap[cj96TBRiscDataGridView.Columns[loop].HeaderText] + ",");
}
csvwriter.WriteLine();


Hope this helps.
 
Share this answer
 
v2
Comments
Branden Coker 13-Jun-14 12:28pm    
I tried your solution phil and I get the message, "the key given does not exist in the dictionary.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900