Reputation: 535
I'm trying to do a combo box that not only contains the drive letter but also the volume label. I'm able to get one or the other by changing the displayMember.
I understand that I need to use .Expression to concatenate them before they go into the combobox. But I'm confused. Should I put the GetDrive into a table first and then do the expression... then load it into the combobox?
Here's the code I have to get one display member:
cmbDestDrive.DataSource = DriveInfo.GetDrives()
.Where(d => d.DriveType == System.IO.DriveType.Removable).ToList();
cmbDestDrive.DisplayMember = "Name";
This displays: F:\
I'd like to display F:\USB Drive
EDIT: Removed a useless line of code.
Upvotes: 0
Views: 539
Reputation: 35363
You only need to Select
the string you want.
var drives = DriveInfo.GetDrives()
.Where(d=>d.IsReady && d.DriveType == System.IO.DriveType.Removable)
.Select(d => d.Name + " (" + d.VolumeLabel + ")" )
.ToList();
cmbDestDrive.DataSource = drives;
No need for DisplayName
Upvotes: 1
Reputation: 22814
I would try something like this:
cmbDestDrive.DataSource = DriveInfo.GetDrives()
.Where(d => d.DriveType == System.IO.DriveType.Removable).Select(n => new { OriginalObject = n, Data = string.Format("{0}|{1}", n.Name, n.VolumeLabel)}).ToList();
cmbDestDrive.DisplayMember = "Data";
It should work (at least I think it should, I can't test it!).
Upvotes: 0