0

我正在使用 Visual Studion 2015、.NET Framework 4.5.2,使用 WPF,并希望以简单的方式将导入的 CSV 文件的内容分配给 DataGrid 对象,如下所述:

<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”

如何摆脱这个问题?

4

2 回答 2

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:看看这是如何做到的并复制(现在很忙,所以我无法进一步详细说明)。取自这里..

通常我们会将 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; }
    }
}
于 2015-09-29T11:42:25.517 回答
1

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

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

您正在尝试添加字符串 (data_col[i]) 而不是 DataGridColumn。编译器将尝试将您提供给方法的内容(字符串)转换为方法所需的内容(DataGridColumn),但它不能这样做,因此出现“无法从'字符串'转换为'System.Windows.Controls”的错误.DataGridColumn'”。

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

于 2015-09-29T11:55:57.473 回答