Search code examples
c#wpfcsvdatagrid

How to avoid zero (0) values when loading a CSV file into DataGrid?


I am working with an app where I load a CSV file into a DataGrid(WPF). What I didn't realize before I started is that it contains zero values which I don't really care about when it comes to displaying. How can I add a check to my code so it filters out the zero values?

//location of CSV File
        string CSVDataBase = @"C:\covid19_confirmed_global.csv";
//create collection for DataGrid source
        async Task<ICollection> CreateDataSource()
        {
            //create new DataTables and rows
            DataTable dataTable = new DataTable();
            DataRow dataRow;

            //create column headers
            dataTable.Columns.Add(new DataColumn("Country/Region", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Province/State", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Number of Cases", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Confirmed Date", typeof(string)));

            //split lines at delimiter ','
            foreach (string Line in await File.ReadAllLinesAsync(CSVDataBase))
            {
                //create new row
                dataRow = dataTable.NewRow();

                //Country/Region
                dataRow[0] = Line.Split(',').ElementAt(1);

                //Province/State
                dataRow[1] = Line.Split(',').ElementAt(0);

                //Number of Cases
                dataRow[2] = Line.Split(',').ElementAt(2);

                //Confirmed Date 
                dataRow[3] = Line.Split(',').ElementAt(3);                

                //add the row created
                dataTable.Rows.Add(dataRow);
            }

            //return dataview 
            DataView dataView = new DataView(dataTable);
            return dataView;
        }
//button load CSV file
        private async void btnDisplayData_Click(object sender, RoutedEventArgs e)
        {
            CovidInfoDataGrid.ItemsSource = await CreateDataSource();
        }

The first two columns in DataGrid are flipped because the first column in the CSV file is Province/State and the second column is Country/Region. In the DataGrid I want them to be the opposite.

I've added the CSV file for better understanding: CSV file

Sample with data besides the zeros: The same CSV file but with meaningful data.

This is how it supposed to look like at the end: The required result


Solution

  • Not sure if I got you right but:

        string[] lines = await File.ReadAllLinesAsync(CSVDataBase);
        string[] dates = lines[0].Split(',');
    
        for (int i = 1; i < lines.Length; i++)
        {
            string[] cells = lines[i].Split(',');
            for (int j = 2; j < cells.Length; j++)
            {
                if (cells[j] == "0")
                {
                    continue;
                }
                dataRow = dataTable.NewRow();
                dataRow[0] = cells[1];
                dataRow[1] = cells[0];
                dataRow[2] = cells[j];
                dataRow[3] = dates[j];
                dataTable.Rows.Add(dataRow);
            }
        }
    

    See if that works - it requires Linq though

    Edited... Ideally you would parse the date to a Date object and pass it then to the row as a date cell. Furthermore, the order of the lines might not be as you indicated it in the screenshot, but once you have it in the dataTable, ordering according to a specific column should be easy.