I have a multi column combo box.When i use it for the first time then it works fine then when i clear it and again enter data into it,all the columns shrink and only 1-2 letters are visible from each row. But I want to display the text such as all the letters in the table are visible,like in the first image.
Code:
namespace MultiColumnComboBoxDemo
{
public class MultiColumnComboBox : ComboBox
{
public MultiColumnComboBox()
{
DrawMode = DrawMode.OwnerDrawVariable;
}
public new DrawMode DrawMode
{
get
{
return base.DrawMode;
}
set
{
if (value != DrawMode.OwnerDrawVariable)
{
throw new NotSupportedException("Needs to be DrawMode.OwnerDrawVariable");
}
base.DrawMode = value;
}
}
public new ComboBoxStyle DropDownStyle
{
get
{
return base.DropDownStyle;
}
set
{
if (value == ComboBoxStyle.Simple)
{
throw new NotSupportedException("ComboBoxStyle.Simple not supported");
}
base.DropDownStyle = value;
}
}
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
InitializeColumns();
}
protected override void OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e);
InitializeValueMemberColumn();
}
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
this.DropDownWidth = (int)CalculateTotalWidth();
}
const int columnPadding = 5;
private float[] columnWidths = new float[0];
private String[] columnNames = new String[0];
private int valueMemberColumnIndex = 0;
private void InitializeColumns()
{
PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties();
columnWidths = new float[propertyDescriptorCollection.Count];
columnNames = new String[propertyDescriptorCollection.Count];
for (int colIndex = 0; colIndex < propertyDescriptorCollection.Count; colIndex++)
{
String name = propertyDescriptorCollection[colIndex].Name;
columnNames[colIndex] = name;
}
}
private void InitializeValueMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
{
valueMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
private float CalculateTotalWidth()
{
float totWidth = 0;
foreach (int width in columnWidths)
{
totWidth += (width + columnPadding);
}
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
if (DesignMode)
return;
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
SizeF sizeF = e.Graphics.MeasureString(item, Font);
columnWidths[colIndex] = Math.Max(columnWidths[colIndex], sizeF.Width);
}
float totWidth = CalculateTotalWidth();
e.ItemWidth = (int)totWidth;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (DesignMode)
return;
e.DrawBackground();
Rectangle boundsRect = e.Bounds;
int lastRight = 0;
//Shakir
//using (Pen linePen = new Pen(SystemColors.GrayText))
using (Pen linePen = new Pen(Color.Black))
{
using (SolidBrush brush = new SolidBrush(e.ForeColor))
//using (SolidBrush brush = new SolidBrush(BackColor))
{
if (columnNames.Length == 0 && e.Index >=0 )
{
e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
}
else
{
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
boundsRect.X = lastRight;
boundsRect.Width = (int)columnWidths[colIndex] + columnPadding;
lastRight = boundsRect.Right;
if (colIndex == valueMemberColumnIndex)
{
using (Font boldFont = new Font(Font, FontStyle.Bold))
{
e.Graphics.DrawString(item, boldFont, brush, boundsRect);
}
}
else
{
e.Graphics.DrawString(item, Font, brush, boundsRect);
}
if (colIndex < columnNames.Length - 1)
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
}
}
}
e.DrawFocusRectangle();
}
}
}
This is the code which fills data into the multicolumn combo box:
SupplierDisplay is a DataTable SupplierMaster is the Table in the database DBRdRw is class
SupplierDisplay = DbRdRw.SqlDbRead("Select SupplierID, SupplierName From SupplierMaster", "SupplierMaster");//data filled to a datatable
//data loading starts
mcbxSupplier.DataSource = SupplierDisplay;
mcbxSupplier.ValueMember = "SupplierName";
mcbxSupplier.DisplayMember = "SupplierName";//ends
add an empty table when u reload the multicolumn combo box just before the SQL statement here is the full code
DataTable dummytable = new DataTable;
mcbxSupplier.DataSource = dummytable;
SupplierDisplay = DbRdRw.SqlDbRead("Select SupplierID, SupplierName From SupplierMaster", "SupplierMaster");//data filled to a datatable
//data loading starts
mcbxSupplier.DataSource = SupplierDisplay;
mcbxSupplier.ValueMember = "SupplierName";
mcbxSupplier.DisplayMember = "SupplierName";//ends