Therk
Therk

Reputation: 391

change with of dropdown window in a telerik RadGridView comboboxcolumn

I'm trying to set the width of the dropdown window in a Telerik RadGridView GridViewComboBoxColumn using C# and WinForms. But I only found ways to set the width of the whole column:

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

But this is not what I want. I want the dropdown window to be wider than the column itself. I also found a DropDownStyle property:

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

But this doesn't make any difference (at least not visually for me) and I couldn't find a property like DropDownWidth or something similar.

Upvotes: 3

Views: 2272

Answers (2)

checho
checho

Reputation: 3120

Here you are:

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

Upvotes: 2

Dave Black
Dave Black

Reputation: 1

Two ideas:

  1. Add DropDownSizingMode to your RadDropDownListEditor. This at least allows the user to manually size the drop down.
  2. Or better yet, use a GridViewMultiComboBoxColumn instead of GridViewComboBoxColumn. This allows you to programmatically size the drop down.

Here's some code to try:

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;
        }
    }
}

Upvotes: 0

Related Questions