• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

GridView to CSV file in C#

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
 
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>
}
 
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;
 
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.
 
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.
 
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.
 
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?
 
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.

 
Back
Top