Menus

Friday, January 6, 2012

Change DataGridView Cell From Text Box To Combo Box

namespace ComboBoxTest {
public partial class Form1 : Form {

    public Form1() {
        InitializeComponent();

        dataGridView1.BorderStyle = BorderStyle.Fixed3D;

        // Add an event handler to validate the
        // cell contents when focus is leaving
        dataGridView1.CellValidating +=
            new DataGridViewCellValidatingEventHandler
                (dataGridView1_CellValidating);

        // Add this event handler to be able to 
        // configure the embedded ComboBox control
        dataGridView1.EditingControlShowing +=
            new DataGridViewEditingControlShowingEventHandler
                (dataGridView1_EditingControlShowing);

        // Change to edit mode when user enters a cell.
        dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;

        // ------------------------------------------------            
        // First create the columns
        dataGridView1.Columns.Add("NameOne", "Column One");
        dataGridView1.Columns.Add("NameTwo", "Column Two");

        // ------------------------------------------------
        // Create a row and add a textbox cell
        DataGridViewRow dataGridRow = new DataGridViewRow();
        DataGridViewCell[] cells = new DataGridViewCell[2];
        DataGridViewTextBoxCell txt1A = new DataGridViewTextBoxCell();
        DataGridViewTextBoxCell txt1B = new DataGridViewTextBoxCell();
        txt1A.Value = "textbox one";
        txt1B.Value = "content one";
        dataGridRow.Cells.Add(txt1A);
        txt1A.ReadOnly = true;
        dataGridRow.Cells.Add(txt1B);
        dataGridRow.Height = 20;
        dataGridView1.Rows.Add(dataGridRow);

        // ------------------------------------------------
        // Add a combobox cell and add the combobox items            
        dataGridRow = new DataGridViewRow();
        cells = new DataGridViewCell[2];
        DataGridViewTextBoxCell txt2A = new DataGridViewTextBoxCell();
        DataGridViewComboBoxCell cbo1 = new DataGridViewComboBoxCell();

        cbo1.Items.Add("Item one");
        cbo1.Items.Add("Item two");
        cbo1.Items.Add("Item three");

        // Set the displayed value.
        // This value needs to be present
        // in the items collection
        cbo1.Value = cbo1.Items[0];

        cbo1.ReadOnly = false;
        txt2A.Value = "a combobox";
        dataGridRow.Cells.Add(txt2A);
        txt2A.ReadOnly = true;
        dataGridRow.Cells.Add(cbo1);
        dataGridRow.Height = 20;
        dataGridView1.Rows.Add(dataGridRow);

        // ------------------------------------------------
        // Add another textbox cell
        dataGridRow = new DataGridViewRow();
        cells = new DataGridViewCell[2];
        DataGridViewTextBoxCell txt3A = new DataGridViewTextBoxCell();
        DataGridViewTextBoxCell txt3B = new DataGridViewTextBoxCell();
        txt3A.Value = "textbox two";
        txt3B.Value = "content two";
        dataGridRow.Cells.Add(txt3A);
        dataGridRow.Cells.Add(txt3B);
        dataGridRow.Height = 20;
        dataGridView1.Rows.Add(dataGridRow);
    }


    /// <summary>
    /// Event handler to allow the embedded control to wire
    /// its own event handlers and/or to set desired style
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void dataGridView1_EditingControlShowing
        (object sender,
        DataGridViewEditingControlShowingEventArgs e) {

        DataGridViewComboBoxEditingControl comboControl
            = e.Control as DataGridViewComboBoxEditingControl;
        if (comboControl != null) {
            // Set the DropDown style to get an editable ComboBox
            if (comboControl.DropDownStyle != ComboBoxStyle.DropDown) {
                comboControl.DropDownStyle = ComboBoxStyle.DropDown;
            }
        }
    }


    /// <summary>
    /// This event handler is called when user has finished 
    /// editing/viewing the current cell
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void dataGridView1_CellValidating
        (object sender, DataGridViewCellValidatingEventArgs e) {

        DataGridViewComboBoxCell cell =
            dataGridView1.CurrentCell as DataGridViewComboBoxCell;
                
        if (cell != null && 
            !cell.Items.Contains(e.FormattedValue)) {
            
            // Insert the new value into position 0
            // in the item collection of the cell
            cell.Items.Insert(0, e.FormattedValue);
            // When setting the Value of the cell, the  
            // string is not shown until it has been
            // comitted. The code below will make sure 
            // it is committed directly.
            if (dataGridView1.IsCurrentCellDirty) {
                // Ensure the inserted value will 
                // be shown directly.
                // First tell the DataGridView to commit 
                // itself using the Commit context...
                dataGridView1.CommitEdit
                    (DataGridViewDataErrorContexts.Commit);
            }
            // ...then set the Value that needs 
            // to be committed in order to be displayed directly.
            cell.Value = cell.Items[0];
        }
    }

}
}

No comments:

Post a Comment