Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to make a program that deletes columns in a DataSet that is filled by an excel file. The way it deletes columns is it compares the header in each column with the first element in each row and deletes the column of any string that does not appear in the rows. My problem is that I'm getting a weird error that I can't understand. it says:

The invocation of the constructor on type 'Excel_Retriever.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.

I'm new to C# and XAML and would really appreciate any help with solving this error. Thank you! Here is my code:

XAML:

<Window x:Class="Excel_Retriever.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="ExcelGrid">
        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True" Height="289"        HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="481" />
    </Grid>
</Window>

C#:

namespace Excel_Retriever
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataSet excel = GetDataTableFromExcel("C:\\Users\\Sweet Lou\\Desktop\\Adjusted research info.xlsx", "Research");
            //dataGrid1.DataContext = excel.Tables[0];
            DataSet ignoreds = Ignore_Names(excel);
            dataGrid1.DataContext = ignoreds.Tables[0];
        }

        public DataSet GetDataTableFromExcel(string FilePath, string strTableName)
        {
            try
            {
                OleDbConnection con = new OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + "; Extended Properties=\"Excel 12.0;HDR=YES;\"");
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }

        public DataSet Ignore_Names(DataSet sheet)
        {
            DataSet ignoreds = sheet;
            DataColumn columnNames = sheet.Tables[0].Columns["Name"]; //first column with names
            //ignoreds.Tables[0].Columns.Add(columnNames);
            int j = 1;
            for (int i = 0; i < 15; i++) //change 15 to variable
            {
                while (String.Compare(columnNames.Table.Rows[i].ToString(), sheet.Tables[0].Columns[j].ColumnName, true) != 0)
                {
                    ignoreds.Tables[0].Columns.RemoveAt(j);
                    j++;
                }
                j++;
            }
            return ignoreds;
        }
    }
}
like image 666
Just Ask Avatar asked Jun 14 '12 05:06

Just Ask


1 Answers

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 :)

like image 160
Gargoyle Avatar answered Nov 12 '22 06:11

Gargoyle