I've got a DataGidView that's very tight on horizontal space, and a column that has rather lengthy descriptions.
What I'd like to do is create a ComboBox column that:
So for example, I have this contrived example below. The real list can have 20-30 items with text that's quite lengthy:
Code Short DropDown Text
1 BigBox Boxes larger than 6' x 6'
2 SmBox Boxes smaller than 6' x 6'
3 BigBrl Barrel 55 gallons
4 SmBrl Barrel less than 55 gallons
So what I want to show is:

When I open the dropdown I want to see:

And of course, when I query the value for the cell, I want "1".
I could split hairs and make the "short" description the first part of the longer description ("BigBx Boxes larger than 6' x 6'") but that doesn't seem right.
I'm looking for suggestions on the best way to accomplish this. No code yet to show, since I'm not quite sure where to start.
You'll want to change the DisplayMember of the combobox on the events DropDown and DropDownClosed.
On the Event DropDown, You'll want to change the DisplayMember to your Long Name. One thing I discovered is that changing the DisplayMember resets the SelectedIndex, so you'll want to save that and then reset it after you've changed the display.
int i = comboBox1.SelectedIndex;
comboBox1.DisplayMember = "LongNm";
comboBox1.SelectedIndex = i;
You'll want to repeat this process with the DropDownClosed event and the Short Name to return to your closed display.
int i = comboBox1.SelectedIndex;
comboBox1.DisplayMember = "ShortNm";
comboBox1.SelectedIndex = i;
The last problem I encountered was changing the width of the combobox dropdown. I found code to do that here. The final code I had looks like this:
public partial class Form1 : Form
{
List<ComboData> data;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
data = new List<ComboData>();
data.Add(new ComboData(1, "BigBox", "Boxes larger than 6x6"));
data.Add(new ComboData(2, "SmBox", "Boxes small than 6x6"));
data.Add(new ComboData(3, "BirBrl", "Barrel 55 Gallons"));
data.Add(new ComboData(4, "smBrl", "Barrel less than 55 gallons"));
comboBox1.DataSource = data;
comboBox1.DisplayMember = "ShortNm";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
int i = comboBox1.SelectedIndex;
comboBox1.DisplayMember = "LongNm";
comboBox1.SelectedIndex = i;
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (ComboData s in ((ComboBox)sender).Items)
{
newWidth = (int)g.MeasureString(s.LongNm, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
int i = comboBox1.SelectedIndex;
comboBox1.DisplayMember = "ShortNm";
comboBox1.SelectedIndex = i;
}
}
public class ComboData
{
public int Code;
public string ShortNm
{
get;
set;
}
public string LongNm
{
get;
set;
}
public ComboData(int c, string s, string l)
{
Code = c;
ShortNm = s;
LongNm = l;
}
}
The end result looks like this:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With