Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
How can i save the contents of a Data Grid View in to a text file? as well as Open it back up?
Posted
Comments
Ger Hayden 1-Aug-11 9:58am    
I am writing up an article on saving a datagrid to XML - I am on leave at the moment but I will resume working on it next week.

1 solution

I don't know whether this is the best solution or not but it works for me.

C#
private void btn_export_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in DataGridViewRow.Rows)
            {
                int cells = dgvr.Cells.Count;
                for (int i = 0; i < cells; i++)
                {
                    File.AppendAllText("contents.txt",dgvr.Cells[i].Value.ToString()+" ");
                }
            }
        }
 
Share this answer
 

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