我使用Visual Studion 2015,.NET Framework 4.5.2,使用WPF,并且想要将导入的CSV文件的内容分配给一个简单的方法来DataGrid对象,在此所说明:无法从'字符串'转换为'System.Windows.Controls.DataGridColumn'
<Grid>
(...)
<DataGrid Name="dgOutput"
CanUserAddRows="True"
CanUserResizeColumns="True"
CanUserSortColumns="True"
Margin="24,142,112,109"
Grid.ColumnSpan="2"
Grid.RowSpan="2"
IsReadOnly="True">
</DataGrid>
</Grid>
我使用下面的方法:
public MainWindow()
{
InitializeComponent();
string[] raw_text = System.IO.File.ReadAllLines("c:\\temp\\import.csv");
string[] data_col = null;
int x = 0;
foreach (string text_line in raw_text)
{
data_col = text_line.Split(',');
if (x == 0)
{
for(int i =0; i <= data_col.Count() -1; i++)
{
dgOutput.Columns.Add(data_col[i]);
}
}
else
{
}
}
}
但是我得到一个错误如下:
CS1503
无法从“字符串”转换为 “System.Windows.Controls.DataGridColumn”
如何摆脱这个问题?
谢谢。但我真的不明白这是如何适合我的CSV导入的东西(我不是一个有经验的开发人员)。 – Joey
@Joey看我的编辑。 –
感谢您付出的额外努力:-)。现在错误消失了,但没有数据显示在网格中;只有4 x 2空列和行。任何额外的想法表示赞赏。 – Joey