简体   繁体   中英

Retrieve images with names from database to ListView

I'm creating a desktop chat app in which a user can search other users by name or email. When I search other users their images are not in a sequential manner.

// This is code from Home Form

    private void BtnSearch_Click(object sender, EventArgs e)
    {
        if (Friends.SelectedIndex == 0)
        {
            UsersClass.SearchContacts(listAllContacts, ImgListAllContacts, TxtSearch.Text.Trim());
        }
    }

// This is Code from UserClass

    public static void SearchContacts(ListView listview, ImageList imagelist, string searchkey)
    {
        DataTable dt = new DataTable();
        listview.Items.Clear();
        ListViewItem[] listviewitem = null;
        dt = DataBaseAccess.Retrive("select UID,FullName,DP from TKDBUsers WHERE ( FullName+' '+UserName ) Like '%" + searchkey + "%' AND UID != '" + LogInUser.UID + "'");
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                listviewitem = new ListViewItem[(dt.Rows.Count)];
                int LC = 0;        //List contacts
                foreach (DataRow item in dt.Rows)
                {
                    Image img = DataBaseAccess.Base64ToImage(Convert.ToString(item["DP"]));
                    imagelist.Images.Add(img);
                    listviewitem[LC] = new ListViewItem(new string[] { Convert.ToString(item["UID"]) + " - " + Convert.ToString(item["FullName"]) }, LC);
                    LC++;
                }
            }
        }
        if (listviewitem != null)
        {
            listview.Items.AddRange(listviewitem);
        }

enter image description here

enter image description here

你不需要清除你的图像清单吗?

Try changing the select query to (add "ORDER BY UserName"):

dt = DataBaseAccess.Retrive("
    SELECT UID, FullName, DP 
    FROM TKDBUsers 
    WHERE (FullName+' '+UserName ) LIKE '%" + searchkey + "%' 
    AND UID != '" + LogInUser.UID + "' 
    ORDER BY UserName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM