在telerik RadGridView组合框中更改下拉窗口

时间:2015-07-13 13:32:29

标签: c# winforms telerik radgridview

我尝试使用C#和WinForms在Telerik RadGridView GridViewComboBoxColumn中设置下拉窗口的宽度。但我只找到了设置整列宽度的方法:

foreach (GridViewColumn col in radGridView1.Columns)
{
    GridViewComboBoxColumn cbCol = col as GridViewComboBoxColumn;
    cbCol.Width = 200;
}

但这不是我想要的。我希望下拉窗口比列本身宽。我还找到了DropDownStyle属性:

cbCol.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
// or this
cbCol.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;

但这并没有任何区别(至少在视觉上对我而言)并且我找不到像DropDownWidth或类似的属性。

2 个答案:

答案 0 :(得分:2)

你在这里:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        radGridView1.CellEditorInitialized += RadGridView1_CellEditorInitialized;
    }

    private void RadGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            RadDropDownListEditorElement ddlElement =(RadDropDownListEditorElement ) editor.EditorElement;
            ddlElement.DropDownMinSize = new Size(200, 300);
        }
    }

答案 1 :(得分:0)

两个想法:

  1. DropDownSizingMode 添加到 RadDropDownListEditor 。这至少允许用户手动调整下拉列表的大小。
  2. 或者更好的是,使用 GridViewMultiComboBoxColumn 而不是 GridViewComboBoxColumn 。这允许您以编程方式调整下拉列表的大小。
  3. 这里有一些代码可供尝试:

    public partial class Form1 : Form
    {
        DataTable dtSrc;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            dtSrc = new DataTable();
            DataColumn dc = dtSrc.Columns.Add("Text");
            dtSrc.Rows.Add("ComboBox Text");
            dtSrc.Rows.Add("Long ComboBox Text");
            dtSrc.Rows.Add("Longer ComboBox Text");
            dtSrc.Rows.Add("Really Longer ComboBox Text");
            dtSrc.Rows.Add("Exceptionally Longer ComboBox Text");
            dtSrc.Rows.Add("A ghastly amount of textual information that is to be used for the dropdown as ComboBox Text");
    
            // ComboBox in Grid
            GridViewComboBoxColumn cbCol = new GridViewComboBoxColumn();
            cbCol.Name = "cbCol";
            cbCol.HeaderText = "CB";
            cbCol.DataSource = dtSrc;
            cbCol.DisplayMember = "Text";
            cbCol.Width = 150;
            this.radGridView1.Columns.Add(cbCol);
    
            // MultiComboBox in Grid
            GridViewMultiComboBoxColumn mcbCol = new GridViewMultiComboBoxColumn();
            mcbCol.Name = "mcbCol";
            mcbCol.HeaderText = "MCB";
            mcbCol.DataSource = dtSrc;
            mcbCol.DisplayMember = "Text";
            mcbCol.Width = 150;
            this.radGridView1.Columns.Add(mcbCol);
    
            // TextBox in Grid
            GridViewTextBoxColumn txtCol = new GridViewTextBoxColumn();
            txtCol.Name = "txtCol";
            txtCol.HeaderText = "TXT";
            txtCol.Width = 400;
            this.radGridView1.Columns.Add(txtCol);
    
        }
    
        private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            if (e.Column == radGridView1.Columns["cbCol"])
            {
                RadDropDownListEditor cboEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
                cboEditor.EditorElement.StretchVertically = false;
                cboEditor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                cboEditor.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            }
    
            if (e.Column == radGridView1.Columns["mcbCol"])
            {
                RadMultiColumnComboBoxElement mcboEditor = (RadMultiColumnComboBoxElement)e.ActiveEditor;
                mcboEditor.EditorControl.Columns["Text"].MinWidth = 300;
                mcboEditor.EditorControl.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                mcboEditor.EditorControl.ShowRowHeaderColumn = false;
                mcboEditor.DropDownMinSize = new Size(350, 150);
                mcboEditor.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            }
        }
    }