How can I programmatically add a multi-column ListViewItem without any data binding?
Asked Answered
A

2

8

I have a ListView with 3 headers, declared in XAML as follows:

<ListView Name="myListView">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="H1"/>
      <GridViewColumn Header="H2"/>
      <GridViewColumn Header="H3"/>
    </GridView>
  </ListView.View>
</ListView>

I want to programmatically add a ListViewItem to this ListView, being able to set the content within the ListViewItem that will go under the first, second, and third columns individually. So far, I have only gotten this far:

ListViewItem l = new ListViewItem();
l.Content = "Content";
myListView.Items.Add(l);

This sets each column to the string "Content". How can I change the code above so that I can add a ListViewItem that will display "Content 1", "Content 2", and "Content 3" under the first, second, and third columns respectively? I tried to look for a SubItem or ListViewSubItem property within ListViewItem, but found nothing.

I assume there's a simple solution, but maybe I'm wrong. Please do not mention data binding, because I only want an answer to the question of programmatically setting the Content property to reflect individual changes in each column.

Thank you very much.

Antagonism answered 30/6, 2010 at 18:1 Comment(0)
A
12

This is not databinding. Think of the Binding statement as giving the column a name.

<ListView Name="myListView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="H1" DisplayMemberBinding="{Binding Col1}"/>
            <GridViewColumn Header="H2" DisplayMemberBinding="{Binding Col2}"/>
            <GridViewColumn Header="H3" DisplayMemberBinding="{Binding Col3}"/>
        </GridView>
    </ListView.View>
</ListView>

In code:

myListView.Items.Add(new { Col1 = "test1", Col2 = "Test2", Col3="test3"});
Atbara answered 30/6, 2010 at 18:25 Comment(2)
This will only work when the number of columns is known at compile time. I have still not found a solution to add an array of strings whose count is known at runtimeMonotheism
Hey @Monotheism - You can do this programmatically in your code behind, but I don't think you can do it declaratively in your XAML.Atbara
S
2

In here is what you do.

You have to set the column headins first, otherwise nothing will show. The add the list view items using a string array.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        listView1.View=View.Details;
        //Set Columns
        listView1.Columns.Add("Col1");
        listView1.Columns.Add("Col2");
        listView1.Columns.Add("Col3");
        //Fill Rows
        ListViewItem lvi;
        lvi=new ListViewItem(new string[] { "A", "B", "C" });
        listView1.Items.Add(lvi);
        lvi=new ListViewItem(new string[] { "D", "E", "F" });
        listView1.Items.Add(lvi);
        lvi=new ListViewItem(new string[] { "G", "H", "I" });
        listView1.Items.Add(lvi);
    }
}

a screenshot of the result is

Scr

Selfsupporting answered 30/6, 2010 at 18:42 Comment(3)
I'm getting this error: 'System.Windows.Controls.ListViewItem' does not contain a constructor that takes '1' arguments.' I'm using WPF. Perhaps I forgot to specify that. I wish your solution worked though. It would make it so much easier.Antagonism
Could you please update the image link? It's dead right now. ThxJulee
This answer works only for winforms. Updated the answer with new screen shot.Selfsupporting

© 2022 - 2024 — McMap. All rights reserved.