1

I'm populating a ListView from a SQL database. The listview has 3 columns defined into the XAML.

<ListView x:Name="lstAS7" Grid.Row="1">
<ListView.View>
    <GridView>
        <GridViewColumn x:Name="AS7Nom"  Header="{DynamicResource AS7_Nom}" Width="350" DisplayMemberBinding="{Binding AS7_Nom}"/>
        <GridViewColumn x:Name="AS7Lib"  Header="{DynamicResource AS7_Lib}" Width="350" DisplayMemberBinding="{Binding AS7_Lib}"/>
        <GridViewColumn x:Name="AS7Prix" Header="{DynamicResource AS7_Prix}" Width="80" DisplayMemberBinding="{Binding AS7_Prix}"/>
    </GridView>
</ListView.View>

The code behind is the next one

foreach (DataRow valeur in ds.Tables["TB1"].Rows)
{
    lstAS7.Items.Add(new { AS7_Nom = valeur["NAME"], AS7_Lib = valeur["TEXT_SHORT"], AS7_Prix = valeur["PRICE"] });
}

For the test I add a button to retrieve the content of the selecteditems and show it on the screen with a messagebox. This is the result.

{ AS7_Nom = "DVI_AT_HS_unit_07242802", AS7_Lib = "Set réservoirs de force ; Ht de meuble: 676 - 800mm ; pour système AVENTOS HS", AS7_Prix = 0 }

How can I retrieve the info from 1 column, for expl the column "AS7_Nom"

MAXE
  • 4,978
  • 2
  • 45
  • 61
Mike
  • 13
  • 3

1 Answers1

0

Since you are adding anonymous type objects to the Items collection of the ListView you could use the dynamic keyword to be able to retrive the property values:

dynamic selectedItem = lstAS7.SelectedItem;
if(selectedItem != null)
    MessageBox.Show(selectedItem.AS7_Nom.ToString());

The other option would be to define a class with AS7_Nom, AS7_Lib and AS7_Prix properties and add intances of this class to the ListView:

lstAS7.Items.Add(new YourClass { AS7_Nom = valeur["NAME"].ToString(), AS7_Lib = valeur["TEXT_SHORT"].ToString(), AS7_Prix = valeur["PRICE"].ToString() });

You could then cast the SelectedItem property to YourClass and access the properties in a compile-time safe fashion:

YourClass selectedItem = lstAS7.SelectedItem as YourClass;
if(selectedItem != null)
    MessageBox.Show(selectedItem.AS7_Nom);
mm8
  • 163,881
  • 10
  • 57
  • 88