0

I am using Visual Studion 2015, .NET Framework 4.5.2, working with WPF, and want to assign the content of an imported CSV-file in a simple way to a DataGrid object, which is described here:

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

I am using the following method:

    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
            {

            }
        }
    }

However I get an error as following:

CS1503
cannot convert from 'string' to 'System.Windows.Controls.DataGridColumn'

How to get rid of this problem?

Bas
  • 1,946
  • 21
  • 38
Joey
  • 511
  • 6
  • 20

2 Answers2

1

You are mixing up adding a column and adding a row.

Try something like this.

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!");

Edit: Try something like (adding one text column and putting data in that one column).

// 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: See how this is doing it and replicate (busy now soz I cannot elaborate further). Taken from here..

Usually we would bind datagrid ItemsSource to a list of data type. I created an example that has binding and manually add items samples for you as reference. Hope it helps.

Markup:

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

Code:

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; }
    }
}
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • Thanks. But I honestly do not understand how this fits in my CSV-import-thing (I am not an experienced developer). – Joey Sep 29 '15 at 11:56
  • Thanks for the extra mile you went :-). Error is gone now, however no data are shown in the grid; only 4 x 2 empty columns and rows. Any additional idea is appreciated. – Joey Sep 29 '15 at 12:42
  • @Joey Your welcome. My bad, you must use 'binding'... have a read of http://wpftutorial.net/DataGrid.html to get the idea... see my edit for an idea. – Paul Zahra Sep 29 '15 at 13:59
  • 1
    Databinding is the way to go, it will make your life easier in development, Edit 2: is the way to go. – Donald Jansen Sep 29 '15 at 14:19
1

The Columns property of a DataGrid is an ObservableCollection of DataGridColumn objects. Its Add() method therefore expects an instance of the DataGridColumn type. In the line

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

You are attempting to add a string (data_col[i]) instead of a DataGridColumn. The compiler will try to convert what you give the method (a string) to what the method requires (a DataGridColumn), but it cannot do that, hence the error that it "cannot convert from 'string' to 'System.Windows.Controls.DataGridColumn'".

What you want to do is add a DataGridTextColumn (which derives from DataGridColumn and will therefore be accepted by the Add() method) for each column in your CSV file (often, the first line of text in a CSV file consists solely of column names which you can use as values for the DataGridTextColumn.Header property).

Bas
  • 1,946
  • 21
  • 38