A constructor call on a type that matches the specified binding constraints throws an exception

I am trying to create a program that deletes columns in a DataSet that is populated with an excel file. The way to delete columns is to compare the heading in each column with the first element in each row and delete the column of any row that does not appear in the rows. My problem is that I get a strange error that I can not understand. He says:

A constructor call of the type "Excel_Retriever.MainWindow" that matches the specified binding constraints made an exception. "Line number" 3 "and line position" 9 ".

I am new to C # and XAML and will be very grateful for your help in resolving this error. Thanks! 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> 

FROM#:

 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; } } } 
+6
source share
1 answer

You do not need to pass strTableName to your method since you never use it.

If you use @ "for strings, you don’t need to avoid things: @" c: \ users .... ";

You get an exception because you are trying to pin strings that don't actually exist. This is what your method should look like if I correctly understand your goal here.

  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); } 

In addition, in the MainWindow constructor, you should simply do this after installing excel:

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

Note that I am setting the ItemsSource, not the DataContext, and I am passing the DefaultView. You can completely remove the ItemsSource binding from XAML.

You really should use VSTO instead of DataSets, but this is one more thing to find out :)

+3
source

Source: https://habr.com/ru/post/918059/


All Articles