I try to fill ListView with custom DataTable. I want to create columns and rows dynamically, from strings (toParse contains rows, toParse2 contains column names). It doesnt seems to work. When I start the program I see smthing like this (System.Data.DataRowView): RESULT
public partial class MainWindow : Window
{
ListView items;
string toParse = "1 12 13\n2 15 16\n3 9 14\n20 123 541235\n4 1234 567";
string toParse2 = "id value1 value2";
public MainWindow()
{
InitializeComponent();
items = GenerateListView(10,10);
}
public ListView GenerateListView(int posx, int posy)
{
ListView listview = new ListView();
DataTable table = new DataTable();
string[] columnNames = toParse2.Split(' ');
foreach (string name in columnNames) table.Columns.Add(name);
string[] lines = toParse.Split('\n');
foreach(string line in lines) {
string[] values = line.Split(' ');
if (values.Length==columnNames.Length)
{
DataRow row = table.NewRow();
table.Rows.Add(row);
for (int i=0; i<values.Length; i++)
{
row[i] = values[i];
}
}
}
listview.ItemsSource = table.DefaultView;
this.grid1.Children.Add(listview);
return listview;
}
}
In addition, when I debug table it seems that Count of Rows actually works, but the List is null(?).
ListViewdoes not have any automatic mechanism for generating column layout based onDataTable. You could generate columns manually or useDataGrid, which can auto generate columns.