none
how to get listview subitems in wpf

    Question

  • hi,

        I have a problem in get listview subitem values in wpf..i m using following coding but i got an error please help....

    private void lvUsers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                cmpy.companypro = lvUsers.SelectedItems[0].SubItems[1].Text;

           }

    error in subitems..but it works in winforms.

    Wednesday, July 17, 2013 10:33 AM

Answers

  • I see... you're using data tables, not objects. OK, then try this:

    string str = (string)((DataRowView)lvUsers.SelectedItems[0])["employername"];
    I believe that you want the employer name so I used that column in my example. Of course, you can get any table column you want instead.
    • Marked as answer by Nandakumar A Wednesday, July 17, 2013 3:49 PM
    Wednesday, July 17, 2013 3:17 PM

All replies

  • The WPF ListView control doesn't have the concept of subitems. If you need the text from a particular column then get it from the object the item is bound to. Something like:

    cmpy.companypro = ((YourDataObject)lvUsers.SelectedItems[0]).YourCompanyProperty;

    Wednesday, July 17, 2013 10:41 AM
  • can give some example coding..
    Wednesday, July 17, 2013 2:02 PM
  • I already presented an example, I'm not sure what I can add to it. Replace YourDataObject and YourCompanyProperty with the relevant class/property and it should work.
    Wednesday, July 17, 2013 2:19 PM
  • may i know what is the YourCompanyProperty...
    Wednesday, July 17, 2013 2:26 PM
  • Hmm, the property where the ListView gets its data for column 1.

    How are you displaying data in the ListView?

    Wednesday, July 17, 2013 2:30 PM
  • through database i displaying listview...

    db coding..

    public void viewcompanydetails()
            {
                con = new SqlConnection(cn);
                con.Open();
                SqlCommand cmd = new SqlCommand("lvcompanydetails", con);
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                lvUsers.DataContext = dt.DefaultView;
                con.Close();
            }

    xaml coding..

    <ListView Margin="10" Name="lvUsers" Width="Auto" Height="auto" MouseDoubleClick="lvUsers_MouseDoubleClick"  ItemsSource="{Binding Path=Table}" SelectionMode="Single">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Employer Id" Width="120" DisplayMemberBinding="{Binding Path=employerid}" />
                            <GridViewColumn Header="Employer" Width="150" DisplayMemberBinding="{Binding Path=employername}" />
                            <GridViewColumn Header="Phone No" Width="150" DisplayMemberBinding="{Binding Path=employerphone}" />
                            <GridViewColumn Header="Email Id" Width="150" DisplayMemberBinding="{Binding Path=employermailid}" />
                            <GridViewColumn Header="Address" Width="150" DisplayMemberBinding="{Binding Path=employeraddr}" />
                            <GridViewColumn Header="Website" Width="150" DisplayMemberBinding="{Binding Path=employerweb}" />
                        </GridView>
                    </ListView.View>
                </ListView>

    i implement your concept but.....

    string str = ((string)lvUsers.SelectedItems[0].what property i put);

    Wednesday, July 17, 2013 3:03 PM
  • I see... you're using data tables, not objects. OK, then try this:

    string str = (string)((DataRowView)lvUsers.SelectedItems[0])["employername"];
    I believe that you want the employer name so I used that column in my example. Of course, you can get any table column you want instead.
    • Marked as answer by Nandakumar A Wednesday, July 17, 2013 3:49 PM
    Wednesday, July 17, 2013 3:17 PM
  • thank u so much i got a output....and i have one more problem....i m using richtext box to address column....if i display to listview ..the listview row size will be changed according to address of the data
    Wednesday, July 17, 2013 3:25 PM
  • That's probably because you have newlines in the address text, you'll have to remove them somehow. The easy way would be to go through the whole data table and remove the newlines from all the address strings.

    But the more "elegant" and WPF way is probably to use a value converter. Add the following class to your project:

        public class AddressConverter : IValueConverter {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
                string address = (string)value;
                return address.Replace("\n", " ");
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
                return value;
            }
        }
    

    This is the converter, it gets a string and replaces the \n characters with spaces.

    Now you need to use this converter in XAML. For this you need to put an instance of the converter in your window's resources: 

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:l="clr-namespace:WpfApplication1">
        <Window.Resources>
            <l:AddressConverter x:Key="AddressConverter" />
        </Window.Resources>
    

    Note that WpfApplication1 used in xmlns:l attribute is the namespace of the converter class, change that to whatever namespace you are using.

    Now you can specify this converter in the binding used by the address column:

    <GridViewColumn Header="Address"
                    Width="150"
                    DisplayMemberBinding="{Binding Path=employeraddr, Converter={StaticResource AddressConverter}}" />
    

    Wednesday, July 17, 2013 3:59 PM