我正在尝试将.CSV文件中的数据导入WPF应用程序中的DataGrid。
这是我的代码。问题是,当我单击按钮并选择文件时,代码会抛出异常“PropertyPath中的语法错误'不匹配的括号”。
此代码有什么问题?
private void OnImport(object sender, RoutedEventArgs e)
{
try
{
lFnLoadFileData();
}
catch (Exception)
{
throw;
}
}
void lFnLoadFileData()
{
try
{
Microsoft.Win32.OpenFileDialog lObjFileDlge = new Microsoft.Win32.OpenFileDialog();
lObjFileDlge.Filter = "CSV Files|*.xlsx";
lObjFileDlge.FilterIndex = 1;
lObjFileDlge.Multiselect = false;
string fName = "";
bool? lBlnUserclicked = lObjFileDlge.ShowDialog();
if (lBlnUserclicked != null || lBlnUserclicked == true)
{
fName = lObjFileDlge.FileName;
}
if (System.IO.File.Exists(fName) == true)
{
// FileStream lObjFileStream = lObjFileDlge.File.OpenRead();
StreamReader lObjStreamReader = new StreamReader(fName);
System.Windows.MessageBox.Show(lObjStreamReader.ToString());
lFnGenerateData(lObjStreamReader);
lObjStreamReader.Close();
}
}
catch (Exception)
{
throw;
}
}
void lFnGenerateData(StreamReader aReader)
{
try
{
bool lBlnIsColumns = true;
string[] lArrCols = null;
List<Contacts> lstPersonalList = new List<Contacts>();
dataGrid1.Columns.Clear();
while (aReader.Peek() > 0)
{
string lStrLine = aReader.ReadLine();
if (lStrLine == null)
break;
if (lStrLine.Trim() == "")
continue;
string[] lArrStrCells = null;
lArrStrCells = lStrLine.Split(';');
if (lArrStrCells == null)
continue;
if (lBlnIsColumns)
{
lArrCols = lArrStrCells;
foreach (string lStrCell in lArrStrCells)
{
DataGridTextColumn lDGCol = new DataGridTextColumn();
lDGCol.Header = lStrCell;
lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);
dataGrid1.Columns.Add(lDGCol);
}
lBlnIsColumns = false;
continue;
}
if (lArrCols == null)
continue;
int lIntColID = 0;
Contacts objPersonalInfo = new Contacts();
objPersonalInfo.ContactName = lArrStrCells[0];
objPersonalInfo.ContactNumber = lArrStrCells[1];
objPersonalInfo.Status = lArrStrCells[2];
lstPersonalList.Add(objPersonalInfo);
}
aReader.Close();
dataGrid1.ItemsSource = lstPersonalList;
}
catch (Exception)
{
throw;
}
}
答案 0 :(得分:0)
我认为问题可能是由这条线造成的。您正在尝试绑定到无效的属性名称?
lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);