0

I have a listview table see the picture in the link

What i want to do is to change the column x in row y programmatically. I am using wpf and a listview with the following xaml code.

`<ListView x:Name="listView1" Height="153" Width="444">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header ="Code" Width="148"></GridViewColumn>
                        <GridViewColumn Header ="Name" Width="148"></GridViewColumn>
                        <GridViewColumn Header ="Country" Width="148"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

` What i want to do is to change the column x in row y programmatically. Something like this listview.Items[x].ColumnIndex[y] = "My value"; I need to pass a string value and i am not using databinding there.

user1520812
  • 81
  • 1
  • 8
  • IF I understood well, this is what You want : http://stackoverflow.com/questions/29483660/how-to-transpose-matrix I am not using WPF, but if this is what You want, you may continue your research bit more, before your question has exact answer – mike_cus Mar 25 '17 at 12:37

1 Answers1

1

The picture and your XAML markup don't quite match. I highly recommend reading over the explanation of how ListViews work here: http://www.wpf-tutorial.com/listview-control/simple-listview/

When changing what shows in a ListView, one modifies the ItemsSource and its contents, not the visual "cell". This is best done with data binding.

Let's say your ListView is setup like this:

XAML:

<ListView x:Name="myListView">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Code}" Header="Code"/>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Country}" Header="Country"/>
        </GridView>
    </ListView.View>
</ListView>

C#:

public class MyInfo
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ...

        ObservableCollection<MyInfo> items = new ObservableCollection<MyInfo>();
        items.Add(new MyInfo() { Code = "mycode1", Name = "myname1", Country = "mycountry1" });
        items.Add(new MyInfo() { Code = "mycode2", Name = "myname2", Country = "mycountry2" });
        items.Add(new MyInfo() { Code = "mycode3", Name = "myname3", Country = "mycountry3" });

        myListView.ItemsSource = items;
    }
}


To change the Name value in the second column, you would use:

items[1].Name = "mynewname2";
TiberiumFusion
  • 348
  • 4
  • 17