You must Sign In to post a response.
  • Category: .NET

    Error: Unable to cast object of type in c#.net windows application

    Hi,

    When I click on the datagridview RowHeaderMouseClick (c#.net windows application), I am getting the following error message:

    Unable to cast object of type 'System.Windows.Forms.DatagridviewTextBoxEditingControl' to 'System.Windows.Forms.ComboBox'.

    My code on RowHeaderMouseClick:

    private void dgvGatePassEntry_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
    if (MessageBox.Show("Are you sure to delete this row?", "Confirmation!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    return;
    Cursor = Cursors.Default;

    if (dgvGatePassEntry.SelectionMode == DataGridViewSelectionMode.RowHeaderSelect)
    {
    dgvGatePassEntry.Rows.RemoveAt(dgvGatePassEntry.CurrentCell.RowIndex);
    }
    }

    Please find attached file on next thread.
  • #762139
    Hi,

    Please find my attached screenshot.

    Thanks,
    Ram Prasad

    Delete Attachment

  • #762140
    Hi

    What is DatagridviewTextBoxEditingControl is it a textbox or what.

    post the code where you tried to assign DatagridviewTextBoxEditingControl to ComboBox control.

    Regards

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762142
    Hi,

    Please see see my code where I am getting error:

    private void dgvGatePassEntry_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    try
    {
    int i = dgvGatePassEntry.CurrentRow.Index;
    dgvGatePassEntry.BeginEdit(true);

    //for Combobox
    if (dgvGatePassEntry.Rows[i].Cells[0].Selected == true || dgvGatePassEntry.Rows[i].Cells[2].Selected == true)
    {
    dgvGatePassEntry.BeginEdit(true);
    ComboBox comboBox = (ComboBox)dgvGatePassEntry.EditingControl; //Getting error here
    if (comboBox != null)
    {
    comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
    comboBox.DroppedDown = true;
    }
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

    Thanks,
    Ram Prasad

  • #762156
    Hello Ram Prasad.

    Try this code :

    GridView.EditMode = EditOnEnter

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
    ComboBox ctl = e.Control as ComboBox;
    ctl.Enter -= new EventHandler(ctl_Enter);
    ctl.Enter += new EventHandler(ctl_Enter);

    }

    void ctl_Enter(object sender, EventArgs e)
    {
    (sender as ComboBox).DroppedDown = true;
    }


    You can try this also :

    make sure e.control is a combobox before casting it

    if (e.control is ComboBox)
    {
    ComboBox editingComboBox = (ComboBox)e.Control;
    if ((editingComboBox != null))
    {
    editingComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
    editingComboBox..DroppedDown = true;
    }
    }


    Refer the below link :

    http://www.wpthm.com/t/tshrbjsjbb6rr17j1kitsmt38bi6jkmr


    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments