We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get Selected Item in single listview selection

Hi, 
How to get the selected value from a listview?
My code:

private void sfListView1_DoubleClick(object sender, EventArgs e)
        {


            //Texto da seleção
            string Seleção = sfListView1.SelectedItems[0].ToString();
            //Pego o número do pedido
            /*string Pedido = Seleção.Substring(0, Seleção.IndexOf("-"));
            string Cliente = Seleção.Substring(Seleção.IndexOf("-"), Seleção.IndexOf("-"));
            */
            MessageBox.Show(Seleção);


        }


The code returns the name of the collection and not the selected item



5 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team July 19, 2019 10:29 AM UTC

Hi Matheus, 
 
Thanks for using Syncfusion product. 
 
We have checked the reported issue in our end and we found you directly show the selected item in message. Instead that you can cast the selected item to particular type of data which you bind with control as like the below code snippet. Here we populated the datasource with the collection of USState type, so we can cast the SelectedItem to particular type. 
 
void sfListView1_DoubleClick(object sender, EventArgs e) 
{ 
    //Texto da seleção 
    string Seleção = (sfListView1.SelectedItems[0] as USState).LongName.ToString(); 
    
    MessageBox.Show(Seleção); 
} 
 
We have prepared the simple sample to achieve your requirement. 
 
 
Please refer the below screen shot for your reference. 
 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan 



MA Matheus July 20, 2019 12:53 PM UTC

This work! Thanks!


JP Jagadeesan Pichaimuthu Syncfusion Team July 22, 2019 06:39 AM UTC

Hi Matheus, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Jagadeesan 



MO Mohamad May 1, 2023 09:50 AM UTC

hi

I set (Datatable) as a datasource for SfListView how to get the selected value for the selected item??



RS Raghavendra Sudhakar Syncfusion Team May 3, 2023 11:04 AM UTC

Hi Mohamad


We have created a sample based on your requirement. Please refer to the sample for your reference.


Code Snippet:


private void SfListView1_SelectionChanged(object sender, Syncfusion.WinForms.ListView.Events.ItemSelectionChangedEventArgs e)

        {

            if (e.AddedItems.Count > 0)

            {

                // Get the selected item's value

                DataRowView selectedRow = (DataRowView)sfListView1.SelectedItem;

                object selectedValue = (object)selectedRow["Name"];

                MessageBox.Show("Selected item's value: " + selectedValue);

            }

        }


Regards,

Raghavendra S




Attachment: ListView_Sample_995d3a2e.zip

Loader.
Live Chat Icon For mobile
Up arrow icon