C# 对与指定绑定约束匹配的类型的构造函数的调用引发异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11027132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The invocation of the constructor on type that matches the specified binding constraints threw an exception
提问by Just Ask
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:
我正在尝试制作一个程序,该程序可以删除由 Excel 文件填充的 DataSet 中的列。它删除列的方式是将每列中的标题与每行中的第一个元素进行比较,并删除未出现在行中的任何字符串的列。我的问题是我遇到了一个我无法理解的奇怪错误。它说:
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'.
对与指定绑定约束匹配的类型“Excel_Retriever.MainWindow”的构造函数的调用引发了异常。行号“3”和行位置“9”。
I'm new to C# and XAML and would really appreciate any help with solving this error. Thank you! Here is my code:
我是 C# 和 XAML 的新手,非常感谢解决此错误的任何帮助。谢谢!这是我的代码:
XAML:
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#:
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;
}
}
}
采纳答案by Gargoyle
You don't need to pass strTableName to your method as you never use it.
您不需要将 strTableName 传递给您的方法,因为您从不使用它。
If you Use @"" for strings you don't need to escape things: @"c:\users....";
如果对字符串使用 @"",则不需要转义:@"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:
此外,在 MainWindow 构造函数中,您应该在设置 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.
请注意,我正在设置 ItemsSource,而不是 DataContext,并且我正在传递 DefaultView。您可以从 XAML 中完全删除 ItemsSource 绑定。
You should really be using VSTOinstead of DataSets, but that's yet another thing to learn :)
你真的应该使用VSTO而不是 DataSets,但这是另一件要学习的事情:)

