2015-09-29 80 views
0

我使用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”

如何摆脱这个问题?

回答

1

你在混合添加一列和添加一行。

尝试类似这样的东西。

DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "Babylon and Ting"; 
// Don't think you want this... textColumn.Binding = new Binding("BabylonAndTing"); 
dgOutput.Columns.Add(textColumn); 

dgOutput.Items.Add("Jah rasterfari!"); 

编辑:试着这么做(添加一个文本列,并把数据在一列)。

// First add a text column. 
DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "Babylon and Ting"; 
dgOutput.Columns.Add(textColumn); 

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++) 
     { 
      // Then add rows to the datagrid. 
      dgOutput.Items.Add(data_col[i]); 
     } 
    } 
    else 
    { 

    } 
} 

EDIT2:看看这是怎么做的,并复制(现在忙SOZ我不能进一步阐述)。取自here ..

通常我们会将datagrid ItemsSource绑定到数据类型列表。我创建了一个绑定并手动添加项目样本作为参考的示例。希望能帮助到你。

标记:

<DataGrid Name="dgOutput" 
      CanUserAddRows="True" 
      CanUserResizeColumns="True" 
      CanUserSortColumns="True" 
      Margin="24,142,112,109" 
      Grid.ColumnSpan="2" 
      Grid.RowSpan="2" 
      IsReadOnly="True"> 
</DataGrid> 

代码:

using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 

namespace SimpleDataGrid 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      DataContext = new List<Person> 
      { 
       new Person{Name = "Tom", Age = 10}, 
       new Person{Name = "Ken", Age = 20}, 
       new Person{Name = "Jen", Age = 30} 
      }; 

      dgOutput.Items.Add(new Person { Name = "Tom", Age = 10 }); 
      dgOutput.Items.Add(new Person { Name = "Ken", Age = 20 }); 
      dgOutput.Items.Add(new Person { Name = "Jen", Age = 30 }); 
      dgOutput.Columns.Add(new DataGridTextColumn { Header = "Name", Binding = new Binding("Name") }); 
      dgOutput.Columns.Add(new DataGridTextColumn { Header = "Age", Binding = new Binding("Age") }); 
     } 
    } 

    public class Person 
    { 
     public string Name { set; get; } 
     public int Age { set; get; } 
    } 
} 
+0

谢谢。但我真的不明白这是如何适合我的CSV导入的东西(我不是一个有经验的开发人员)。 – Joey

+0

@Joey看我的编辑。 –

+0

感谢您付出的额外努力:-)。现在错误消失了,但没有数据显示在网格中;只有4 x 2空列和行。任何额外的想法表示赞赏。 – Joey

1

一个DataGrid的列属性是DataGridColumn对象的一个​​ObservableCollection。它的Add()方法因此需要DataGridColumn类型的一个实例。在线

dgOutput.Columns.Add(data_col[i]); 

您正试图添加一个字符串(data_col [i])而不是DataGridColumn。编译器会尝试将你给的方法(一个字符串)转换为方法需要的东西(一个DataGridColumn),但是它不能这样做,因此错误是它“无法从'字符串'转换为'System.Windows.Controls .DataGridColumn'”。

你想要做的是添加一个DataGridTextColumn(它源自DataGridColumn,因此将被Add()方法接受),用于CSV文件中的每列(通常,CSV文件中的第一行文本包含仅用于可以用作DataGridTextColumn.Header属性值的列名称)。