Link to home
Start Free TrialLog in
Avatar of mphillip85
mphillip85Flag for United States of America

asked on

Export Datagrid to CSV not Datagridview

Export DataGrid to CSV.  I am trying to find the solution to a datagrid instead of a datagridview.  
I am using vb .net 2003.  I saw the datagridview code but I need a conversion of that to fit a datagrid only.

Thanks.
Avatar of surajguptha
surajguptha
Flag of United States of America image

Have thought about iterating throw all the cells in the datagrid and generating a CSV manually?
Avatar of mphillip85

ASKER

I am trying to have a function that I can just pass the datagrid name to and it does the rest.  I basically know how to iterate through the rows and cells, but I cannot figure out how to find the column names and/or how many columns and/or how many rows.  I can get the row count from the dataset that I gave as the datasource to the datagrid.  

I just want to pass a few parameters and generate a CSV.  I am having to do many datagrid reports, that I am trying to make a function call to achieve this.
ok yeah thats a good idea. why not iterate through the dataset you are going to bind to the datagrid and generate the csv??

List<String> csvs = new List<String>();
for(int i=0; i < datatable.Rows.Count ; i++)
{
String rowcsv = "";
for(int columCounter = 0; columnCounter < dataTable.Rows[i].Cells.Count; columnCounter++)
{
rowcsv+= dataTable.Rows[i].Cells[columnCounter].ToString() + "'";
}
csvs.Add(rowcsv)
}

THis is the pseudo code of the function that takes a DataTable as input and returns a CSV array. Improvise on this with your requirements
I would use the dataset except that I do not use all the columns in the dataset to display.  Or else I would have 10 times the datasets I needed to do everything.  I need to just export only what is displayed in the datagrid.  Or else I would have used the dataset and made calls to find the columns and etc.

ASKER CERTIFIED SOLUTION
Avatar of Qaiser_Mehmood_Mughal
Qaiser_Mehmood_Mughal
Flag of Pakistan 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