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?
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; }
}
}