3

我有一个多列组合框。当我第一次使用它时,它工作正常,然后当我清除它并再次输入数据时,所有列都会缩小,每行只能看到 1-2 个字母。但我想显示文本,例如表格中的所有字母都可见,就像第一张图片一样。

在运行时 保存后

代码:

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();

    }








}
}

这是将数据填充到多列组合框中的代码:

SupplierDisplay 是一个 DataTable SupplierMaster 是数据库中的表 DBRdRw 是类

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
4

1 回答 1

1

当您在 SQL 语句之前重新加载多列组合框时添加一个空表这里是完整代码

          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
于 2013-04-12T06:05:09.793 回答