Skip to content

Edit a DataGridView ComboBox Item.

vito

Asked Jun 13, 2006, 12:22 AM EDT1 answer
Share
LinkedInX
I need an editable combo (DropDownCombo style) appears on a DataGridView current cell. 
I tryed setting the column type as 'DataGridViewComboBoxColumn'. It works fine but I can't 
edit ComboBoxItems.   
How can I edit and insert new items direcly from combo ? Is it possible ?
There is any workaround to do ?
Thanks.
Brainjar.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I 've find out this solution. It uses an external 'true' DropDownComboBox.

    .....
    this.cboDescrizione.Leave += new System.EventHandler(this.cboDescrizioneLeave);
    .....
    this.grdWPreventivo.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler( this.GrdWPreventivoCellContentClick);
    this.grdWPreventivo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler( this.GrdWPreventivoCellContentClick);
    .....
    .....
    private void GrdWPreventivoCellContentClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
    {
    DataGridView grd = this.grdWPreventivo;
    Rectangle rect = grd.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);

    if (e.ColumnIndex==0) {
    RigaCorrente = e.RowIndex;
    blEditing = true;
    // -------------------------------------
    // Set combo absolute position
    // -------------------------------------
    cboDescrizione.Left = rect.X + grd.Left;
    cboDescrizione.Top = rect.Y + grd.Top;
    cboDescrizione.BringToFront();
    } else
    blEditing = false;
    // ---------------------------------------
    // Set combo visible, so can receive focus
    // ---------------------------------------
    cboDescrizione.Visible = blEditing;
    // -----------------------------------------
    // Can combo receive Focus() ?
    // -----------------------------------------
    if (blEditing) cboDescrizione.Focus();
    }

    private void cboDescrizioneLeave(object sender, System.EventArgs e)
    {
    // -----------------------------------
    // Stop editing. Start CellValidating.
    // -----------------------------------
    string valore = cboDescrizione.Text;

    blEditing = false;
    this.grdWPreventivo.Rows[RigaCorrente].Cells[0].Value = valore;
    cboDescrizione.SendToBack();
    cboDescrizione.Visible = false ;
    int i = this.cboDescrizione.FindString(cboDescrizione.Text );
    if (i==-1) cboDescrizione.Items.Add(cboDescrizione.Text);
    }


    Bye.
    0