The invocation of the constructor on type that matches the specified binding constraints threw an exception The invocation of the constructor on type that matches the specified binding constraints threw an exception wpf wpf

The invocation of the constructor on type that matches the specified binding constraints threw an exception


You don't need to pass strTableName to your method as you never use it.

If you Use @"" for strings you don't need to escape things: @"c:\users....";

You're getting an exception because you're trying to nuke rows that don't really exist. This is what your method should look like, if I understand your goal here correctly.

    public static void Ignore_Names(DataSet sheet) {        var table = sheet.Tables[0];        var columns = table.Columns;        var nameColumn = columns["Name"];        var names = new List<string>();        foreach (DataRow row in nameColumn.Table.Rows)            names.Add(row[0].ToString().ToLower());        // Work from right to left.  If you delete column 3, is column 4 now 3, or still 4?  This fixes that issue.        for (int i = columns.Count - 1; i >= 0; i--)            if (!names.Contains(columns[i].ColumnName.ToLower()))                columns.RemoveAt(i);    }

Also, in MainWindow constructor, you should just do this after you set excel:

   Ignore_Names(excel);          dataGrid1.ItemsSource = excel.Tables[0].DefaultView; 

Note that I'm setting ItemsSource, not DataContext, and I'm passing the DefaultView. You can remove your ItemsSource binding from the XAML entirely.

You should really be using VSTO instead of DataSets, but that's yet another thing to learn :)