SelectedItem.Text always empty

6 posts, 0 answers
  1. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Oct 2018

    Posted 07 Oct 2018 Link to this post

    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.

  2. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Oct 2018

    Posted 08 Oct 2018 Link to this post

    I'm using DetailsView. So is where something special?
  3. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3988 posts

    Posted 08 Oct 2018 Link to this post

    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.
  4. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Oct 2018

    Posted 09 Oct 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    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.
  5. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3988 posts

    Posted 10 Oct 2018 Link to this post

    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.
  6. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Oct 2018

    Posted 10 Oct 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    [...] .SelectedItem[0] [...] worked for me. Thanks :)
Back to Top