Excel에서 채워진 DataSet의 열을 삭제하는 프로그램을 만들려고합니다. 열을 삭제하는 방법은 각 열의 머리글을 각 행의 첫 번째 요소와 비교하고 행에 나타나지 않는 문자열의 열을 삭제하는 것입니다. 내 문제는 내가 이해할 수없는 이상한 오류가 발생하고 있다는 것이다. 그것은 :지정한 바인딩 제약 조건과 일치하는 형식에서 생성자를 호출하면 예외가 throw되었습니다.
지정한 바인딩 제약 조건과 일치하는 'Excel_Retriever.MainWindow'형식의 생성자 호출이 예외를 던졌습니다. ' 행 번호 '3'및 행 위치 '9'.
저는 C# 및 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 번호 :
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;
}
}
}
랩의 연결 및 어댑터를 배울 수있는 또 다른 일이다. – abatishchev