首页 > 解决方案 > Problem getting selected row values to combo box from datagridview cell click in c#

问题描述

I have two forms Form1 and Form2. I am facing problem loading Datagridview cell value of Form1 to the combo box of Form2 on cell double click event as show in the screenshot. using below code, text box values getting properly from the row i had selected but combo box shows empty.

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex != -1 && e.ColumnIndex != -1)
    {
        Form2 fo = new Form2();
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        fo.textBox1.Text = row.Cells["firstNameGV"].Value.ToString();
        fo.textBox2.Text = row.Cells["lastNameGV"].Value.ToString();
        fo.comboBox1.Text = row.Cells["deptGV"].Value.ToString();
        fo.ShowDialog();
    }
}

screenshot of forms

how do i get the values in combo box. (p.s: combo box on Form2 is filled from database on form_loadevent values can also be seen in the attachment.)

private void Form2_Load(object sender, EventArgs e)
{
    loadItems();
    comboBox1.SelectedIndex = -1;
}

标签: c#winforms

解决方案


推荐阅读