This is a migrated thread and some comments may be shown as answers.

SelectedItem.Text always empty

5 Answers 329 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Sebastian
Top achievements
Rank 1
Sebastian asked on 07 Oct 2018, 09:33 AM

This is how I populate my ListView:

 

List<Users> ListUsers = (List<Users>)FormData.Resp;
 
lv.Items.Clear();
 
foreach (var User in Users)
{
                    ListViewDataItem ListViewDataItem = new ListViewDataItem();
                    lv.Items.Add(ListViewDataItem);
 
                    ListViewDataItem[0] = User.ID.ToString();
                    ListViewDataItem[1] = User.BenutzerID;
                    ListViewDataItem[2] = User.Nachname;
                    ListViewDataItem[3] = User.Vorname;
                    ListViewDataItem[4] = User.EMailAdresse;
}

 

 

After selecting Items, the "Text" property of CurrentItem, SelectedItem or SelectedItems[0] is always empty. I tried also to get the Text by "lv.Items[lv.SelectedIndex].Text", but where is also the text property empty. But the SelectedIndex is correct.

5 Answers, 1 is accepted

Sort by
0
Sebastian
Top achievements
Rank 1
answered on 08 Oct 2018, 12:20 PM
I'm using DetailsView. So is where something special?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Oct 2018, 01:54 PM
Hello, Sebastian, 

When you use ListViewType.DetailsView, note that it is necessary to add columns in the RadListView.Columns collection before adding items. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/listview/populating-with-data/unbound-mode

Here is a sample code snippet which result is illustrate din the below screenshot: 

public RadForm1()
{
    InitializeComponent();
 
    this.radListView1.ViewType = ListViewType.DetailsView;
    for (int i = 0; i < 5; i++)
    {
        this.radListView1.Columns.Add("Col" + i);
    }
 
    List<User> ListUsers = new List<User>();
 
    for (int i = 0; i < 5; i++)
    {
        ListUsers.Add(new User(i,i,"Name" + i,"Last" + i,"email" + i));
    }
    foreach (var User in ListUsers)
    {
        ListViewDataItem ListViewDataItem = new ListViewDataItem();
        this.radListView1.Items.Add(ListViewDataItem);
 
        ListViewDataItem[0] = User.ID.ToString();
        ListViewDataItem[1] = User.BenutzerID;
        ListViewDataItem[2] = User.Nachname;
        ListViewDataItem[3] = User.Vorname;
        ListViewDataItem[4] = User.EMailAdresse;
    }
}
 
public class User
{
    public int ID { get; set; }
 
    public int BenutzerID { get; set; }
 
    public string Nachname { get; set; }
 
    public string Vorname { get; set; }
 
    public string EMailAdresse { get; set; }
 
    public User(int iD, int benutzerID, string nachname, string vorname, string eMailAdresse)
    {
        this.ID = iD;
        this.BenutzerID = benutzerID;
        this.Nachname = nachname;
        this.Vorname = vorname;
        this.EMailAdresse = eMailAdresse;
    }
}



I hope this information helps.

Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sebastian
Top achievements
Rank 1
answered on 09 Oct 2018, 05:54 AM
Thanks for you answer. Display the data is not the problem. By the way, I added the columns at the designer before. The problem is reading the selected text.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Oct 2018, 01:12 PM
Hello, Sebastian, 

The Text property of the ListViewDataItem depends on the applied RadListView.DisplayMember. However,when using ListViewType.DetailsView, it is appropriate to extract the desired value directly from the cell. Here is demonstrated a sample code snippet: 
private void radButton1_Click(object sender, EventArgs e)
{
    Console.WriteLine(this.radListView1.SelectedItem["Col2"]);
}

Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sebastian
Top achievements
Rank 1
answered on 10 Oct 2018, 01:57 PM
[...] .SelectedItem[0] [...] worked for me. Thanks :)
Tags
ListView
Asked by
Sebastian
Top achievements
Rank 1
Answers by
Sebastian
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or