1

This is a continuation of a question that I asked previously

Bind jagged string array to a DataGrid

In my code, I am importing rows from a CSV and storing them in a jagged array. I also ask the user if the first row contains column headers. If it does, then I use that row as the column name. What I can't figure out though is how do I exclude the first row of the array from the DataGrid ItemSource. Here is the method so you can see exactly what I am doing.

    private void Import_Click(object sender, RoutedEventArgs e)
    {
        MessageBoxResult headerChoice = MessageBox.Show("Is the first row a column header?", "Import Options", MessageBoxButton.YesNo);
        string[][] array = fs.CSVToStringArray();
        string[] headerTitles = new string[array[0].Length];
        for (int i = 0; i < array[0].Length; i++)
        {
            var col = new DataGridTextColumn();
            if (headerChoice == MessageBoxResult.Yes)
                col.Header = array[0][i];
            else if (headerChoice == MessageBoxResult.No)
                col.Header = "Column " + i;
            col.Binding = new Binding(string.Format("[{0}]", i));
            this.ExternalData._dataGrid.Columns.Add(col);
        }
        if (headerChoice == MessageBoxResult.Yes)
            //exclude first row of array;
        else if (headerChoice == MessageBoxResult.No)
            this.ExternalData._dataGrid.ItemsSource = array;
    }

Where I have the comment "exclude first row of array" I want to put some code that binds the array to the ItemSource and excludes the first row, because it is a column header.

Community
  • 1
  • 1
Adam
  • 1,483
  • 4
  • 21
  • 50

2 Answers2

1

Using Array.Copy() function would be more concise rather than using for loop to copy data between two arrays :

if (headerChoice == MessageBoxResult.Yes)
{
    string[][] arrayNoHeader = new string[array.Length - 1][];
    //copy data from array to arrayNoHeader starting from index 1 (skipping row 0)
    Array.Copy(array, 1, arrayNoHeader, 0, array.Length-1);
    this.ExternalData._dataGrid.ItemsSource = arrayNoHeader;
}
har07
  • 88,338
  • 12
  • 84
  • 137
0

Here is one method I came up with, although I don't know if this is the most efficient way so please if you have another suggestion let me know.

    private void Import_Click(object sender, RoutedEventArgs e)
    {
        MessageBoxResult headerChoice = MessageBox.Show("Is the first row a column header?", "Import Options", MessageBoxButton.YesNo);
        string[][] array = fs.CSVToStringArray();
        for (int i = 0; i < array[0].Length; i++)
        {
            var col = new DataGridTextColumn();
            if (headerChoice == MessageBoxResult.Yes)
                col.Header = array[0][i];
            else if (headerChoice == MessageBoxResult.No)
                col.Header = "Column " + i;
            col.Binding = new Binding(string.Format("[{0}]", i));
            this.ExternalData._dataGrid.Columns.Add(col);
        }
        if (headerChoice == MessageBoxResult.Yes)
        {
            string[][] arrayNoHeader = new string[array.Length - 1][];
            for (int i = 0; i < arrayNoHeader.Length; i++)
                arrayNoHeader[i] = array[i + 1];
            this.ExternalData._dataGrid.ItemsSource = arrayNoHeader;
        }
        else if (headerChoice == MessageBoxResult.No)
            this.ExternalData._dataGrid.ItemsSource = array;
    }

Basically I just copy the original data to another array that is one shorter and excludes the first row of the other array.

Adam
  • 1,483
  • 4
  • 21
  • 50
  • I'd say use LINQ projection to project your jagged array even onto a tuple and them using whatever criteria exclude the first row, using Skip/Take approach. – user3455395 Apr 08 '14 at 13:23