mardi 28 juin 2016

C# loading images and strings into one DataGridView/ListView using sql query

Hi guys I'm creating this messenger and I would like a List View or DataGridView which shows the users friend's avatar and name. It works perfectly in a list box but I cannot add images.

List box Code:

            var query = from o in Globals.DB.Friends
                    where o.UserEmail == Properties.Settings.Default.Email
                    select new
                    {
                        FirstName = o.FirstName,
                        LastName = o.LastName,
                        Email = o.Email,
                        Display = string.Format("{0} {1} - ({2})", o.FirstName, o.LastName, o.Email)
                    };
        FriendsLb.DataSource = query.ToList();
        FriendsLb.ClearSelected();

List box display and value members:

            FriendsLb.DisplayMember = "Display";
        FriendsLb.ValueMember = "Email";
        FriendsLb.ValueMember = "FirstName";
        FriendsLb.ValueMember = "LastName";

        Globals.DB.Friends.Clear();
        Globals.DB._UserDetails.Clear();
        Globals.DB.AcceptChanges();

        string friends = string.Format("{0}\Friends\" + Properties.Settings.Default.Email + ".dat", Application.StartupPath);
        if (File.Exists(friends))
            Globals.DB.Friends.ReadXml(friends);

        string user = string.Format("{0}\Users\" + Properties.Settings.Default.Email + ".dat", Application.StartupPath);
        if (File.Exists(user))
            Globals.DB._UserDetails.ReadXml(user);

        FriendsLb.ClearSelected();

DataGridView Code:

            var query = from o in Globals.DB.Friends
                    where o.UserEmail == Properties.Settings.Default.Email
                    select new
                    {
                        FirstName = o.FirstName,
                        LastName = o.LastName,
                        Email = o.Email,
                        Avatar = o.Avatar,
                        Display = o.Avatar + string.Format("{0} {1} - ({2})", o.FirstName, o.LastName, o.Email)
                    };
        FriendsDGV.DataSource = query.ToList();

Problem is: DataGridView doesn't provide display and value members which is needed to load the data.

List View: Doesn't provide data source or display and member values

How can I do this? :(

Aucun commentaire:

Enregistrer un commentaire