1

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(?).

4
  • Can you please share xaml of your view? Commented May 1, 2018 at 6:42
  • <Grid Name="grid1"></Grid> I said I want it to generate dynamically in c# declaration rather than in xaml. Commented May 1, 2018 at 7:01
  • 1
    ListView does not have any automatic mechanism for generating column layout based on DataTable. You could generate columns manually or use DataGrid, which can auto generate columns. Commented May 1, 2018 at 7:12
  • @SolisQQ, I've added an answer for this. Please check it and let me know if it's working fine for you. Commented May 1, 2018 at 7:40

1 Answer 1

1

You need to define your listview columns. You can use GridView to define your columns of ListView.

I've added GridView myGridView = new GridView(); and define its columns using data binding.

public partial class MainWindow : Window
    {
        private ListView items;
        private string toParse = "1 12 13\n2 15 16\n3 9 14\n20 123 541235\n4 1234 567";
        private 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();
            GridView myGridView = new GridView();

            string[] columnNames = toParse2.Split(' ');
            foreach (string name in columnNames)
            {
                table.Columns.Add(name);

                GridViewColumn gvc = new GridViewColumn();
                gvc.DisplayMemberBinding = new Binding(name);
                gvc.Header = name;
                gvc.Width = 100;

                myGridView.Columns.Add(gvc);
            }
            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.View = myGridView;
            listview.ItemsSource = table.DefaultView;

            this.grid1.Children.Add(listview);
            return listview;
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thank you, it works perfect :). I almost abandon this idea and got back to the listBox with string format.
I'm happy to help... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.