0
votes

I have a datagridview(dgv) which holds the display members of 2 combobox, dgv is for showing 1-n n-1 relations between those comboboxes.

I wrote a code for to change the selected values in comboboxes when a row is pressed in the gridview. It is not working all the time, I mean it does update for each column when I press; but do not change them everytime.

For ex: clicking on row 1, 2, 3, 4, nothing happens; start clicking rows and the code start working; then its not working again and so on.

here is the code for changing comboboxes selected value:

private void dgvTypesRelation_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    string empType = Header.returnItemInGrid(dgvTypesRelation.SelectedRows, 0);
    string reqType = Header.returnItemInGrid(dgvTypesRelation.SelectedRows, 1);
    string query1 = "SELECT Request_TypeID FROM Request_Type WHERE Request_Type=@r";
    string query2 = "SELECT Employee_TypeID FROM Employee_Type WHERE Employee_Type=@e";
    int req, emp;
    DataTable result = new DataTable();

    using (SqlConnection conn = new SqlConnection(connStr))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query1, conn))
        {
            cmd.Parameters.AddWithValue("@r", reqType);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                result.Load(dr);
            }
            req = Convert.ToInt32(result.Rows[0]["Request_TypeID"]);

            cmd.CommandText = query2;
            cmd.Parameters.AddWithValue("@e", empType);
            result = new DataTable();
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                result.Load(dr);
            }
            emp = Convert.ToInt32(result.Rows[0]["Employee_TypeID"]);
        }
    }

    cmbxRequestConnect.SelectedValue = req;
    cmbxEmployeeConnect.SelectedValue = emp;
    chkbxRemove.Checked = true;
}
1

1 Answers

1
votes

I'm pretty sure I made this mistake too, once. Try using this code under dgvTypesRelation_CellClick instead of dgvTypesRelation_CellContentClick.

Described by Visual Studio, "CellContentClick: Occurs when the content within the cell is clicked", whereas "CellClick: Occurs when any part of the cell is clicked"

Hope this helps!