none
Export Each Name From DataGrid To Individual CSV RRS feed

  • Question

  • I know how to export an entire datagridview to a csv, but how can I take it a step further and export (based off the 1st column in the DataGrid Name) to an individual csv so that each name is exported to there own csv?

    Sample syntax as such...

        static void Main()
        {
            DataTable information = GetTable();
    		DataGridView dgvAllData = new DataGridView();
    		dgvAllData.DataSource = information;
        }    
    	
    	static DataTable GetTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("ID", typeof(string));
            table.Columns.Add("Item", typeof(string));
    		table.Columns.Add("QTY", typeof(string));
            table.Rows.Add("Jim", "jm123", "Cap", "4");
            table.Rows.Add("Jim", "jm123", "Rocks", "12");
            table.Rows.Add("Jack", "jr123", "Gloves", "1");
            table.Rows.Add("James", "jm33", "Knickers", "4");
            return table;
        }

    So 3 csv files would be generated, jim, jack, james.

    Friday, December 15, 2017 2:15 PM

Answers

  • I would group the results by the column(s) that are used to determine the new csv file (Name it looks like here). Then enumerate each of those rows and add them to the CSV as you would for the entire grid. It really boils down to the same logic except the rows are filtered and the grouping identifies the "filename".

    //Not tested
    var groups = from r in table.Rows.OfType<DataRow>()
                 group r by r.Name into g
                 select g;
    
    foreach (var group in groups)
    {
       //Create new CSV file by group.Key
       
       //Enumerate rows if you're doing it manually or
       //call helper method that accepts the IEnumerable<DataRow>
       foreach (var row in group)
       {
          //Write CSV line
       };
    };


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by IndigoMontoya Friday, December 15, 2017 2:45 PM
    • Unmarked as answer by IndigoMontoya Friday, December 15, 2017 3:25 PM
    • Marked as answer by IndigoMontoya Friday, December 15, 2017 3:29 PM
    Friday, December 15, 2017 2:44 PM
    Moderator

All replies

  • I would group the results by the column(s) that are used to determine the new csv file (Name it looks like here). Then enumerate each of those rows and add them to the CSV as you would for the entire grid. It really boils down to the same logic except the rows are filtered and the grouping identifies the "filename".

    //Not tested
    var groups = from r in table.Rows.OfType<DataRow>()
                 group r by r.Name into g
                 select g;
    
    foreach (var group in groups)
    {
       //Create new CSV file by group.Key
       
       //Enumerate rows if you're doing it manually or
       //call helper method that accepts the IEnumerable<DataRow>
       foreach (var row in group)
       {
          //Write CSV line
       };
    };


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by IndigoMontoya Friday, December 15, 2017 2:45 PM
    • Unmarked as answer by IndigoMontoya Friday, December 15, 2017 3:25 PM
    • Marked as answer by IndigoMontoya Friday, December 15, 2017 3:29 PM
    Friday, December 15, 2017 2:44 PM
    Moderator
  • @CoolDadTx -> this seems to export the DataTable to individual csv files - what about doing such from the datagrid?
    Friday, December 15, 2017 3:25 PM
  • A datagrid is for display only. It is always backed by a data source. Your export should be from the data source, not the UI. 

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 15, 2017 3:29 PM
    Moderator