GridView to CSV file in C#

Jun 2, 2008
163
0
0
I'm trying to convert a GridView to CSV

All I have is the GridView displaying my XML file.

//**************

protected void Page_Load(object sender, EventArgs e)
{

DataSet myDataSet = new DataSet();

myDataSet.ReadXml(Server.MapPath("portfolio.xml"));

//dgBooks.DataSource = myDataSet;
//dgBooks.DataBind();

GridView1.DataSource = myDataSet;
GridView1.DataBind();

}

//**************

Can someone point me out to a general guide. I've usually only found ones that are specific for a certain subject and I don't understand them.

Thanks
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You can always iterate through the rows in your GridView and spit out a row of csv to text. Something like

foreach (GridViewRow row in myGridView) {
<insert file writing code here>
}
 
Jun 2, 2008
163
0
0
That spit out an error... I was able to do this

foreach (GridViewRow row in GridView1.Rows)
{

}

Does that allow me to iterate through? I was able to get a count... with int = int + 1;
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Well to be honest, you shouldn't be grabbing the data from the GridView to spit out to your csv file. You should have a set of objects that is storing all of your data that is independent of any GUI and should be using those objects to persist data to the HDD.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Originally posted by: TheEarthWillShake
That spit out an error... I was able to do this

foreach (GridViewRow row in GridView1.Rows)
{

}

Does that allow me to iterate through? I was able to get a count... with int = int + 1;

Yeah, but once you have the Rows, you need to then do a foreach on all the columns. Building a string to write out to the file.
 

GoatMonkey

Golden Member
Feb 25, 2005
1,253
0
0
If you already have the XML, can't you just navigate through the XML to generate your CSV file? If you happen to already have a class that matches the structure of the XML for deserializing, you could loop through that to make your CSV.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Originally posted by: TheEarthWillShake
That spit out an error... I was able to do this

foreach (GridViewRow row in GridView1.Rows)
{

}

Does that allow me to iterate through? I was able to get a count... with int = int + 1;
Yeah, the .Rows property probably returns a RowCollection object (or something like that) that you can iterate through with a foreach() loop.

You could do something like this:

// your final CSV result will be stored here
StringBuilder strBuilder = new StringBuilder();

foreach (GridViewRow r in GridView1.Rows)
{
.....foreach (TableCell c in r.Cells)
.....{
..........// get cell's text
..........string cellText = c.Text;

..........// add quotes and comma around value and append
..........strBuilder.Add("\"" + cellText + "\",");
.....}

.....// insert a newline character after every row
.....strBuilder.Add(Environment.NewLine);
}

// output CSV result
Console.WriteLine(strBuilder.ToString());

That's one quick and easy way of doing it. Not the most elegant solution, haha, but I think it would work.

Originally posted by: Crusty
Well to be honest, you shouldn't be grabbing the data from the GridView to spit out to your csv file. You should have a set of objects that is storing all of your data that is independent of any GUI and should be using those objects to persist data to the HDD.
This would be a better solution though.

By the way, is the fucking "Attach Code" feature ever going to work?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: clamum

By the way, is the fucking "Attach Code" feature ever going to work?
I'm pretty sure Derek Wilson said not until we get away from this crappy Fusetalk.