Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView with multiple Rows with 3 ComboBoxColumns. If i change the first ComboBoxColumn on third Row it should only affect the second ComboBox on the third Row where i made a selection. Heres a Example

Link to image

When i change something on the highlighted (gray) row it should only affect the ComboBox on the gray row.

What I have tried:

Can you please help me out with some Ideas?

C#
private String id;
private int row_number;

//Fill first Combo
private void fill_first_combo()
        {
            datagridview_col1.Items.Clear();

            if (datagridview.SelectedRows.Count > 0)
            {
                string rcs = db_conn.connection();

                using (var OraConn = new OracleConnection(rcs))
                {
                    using (var OraCmd = OraConn.CreateCommand())
                    {
                        try
                        {
                            OraConn.Open();
                            OraCmd.BindByName = true;

                            OraCmd.CommandText = "Oracle Command"
                            OracleDataReader OraDataReader = OraCmd.ExecuteReader();
                            if (OraDataReader.Read() == false)
                            {
                                //MessageBox
                            }
                            else
                            {
                                using (var OraDat = new OracleDataAdapter(OraCmd))
                                {
                                    using (var table = new DataTable())
                                    {
                                        OraDat.Fill(table);

                                        foreach (DataRow row in table.Rows)
                                        {
                                            foreach (DataColumn column in table.Columns)
                                            {
                                                if (row[column] != null)
                                                {
                                                    if (column.ColumnName == "COLUMNNAME")
                                                    {
                                                        datagridview_col1.Items.Add(row[column.ColumnName].ToString());
                                                        id = row[column.ColumnName].ToString().Substring(0, 6);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (OracleException ex)
                        {
                           //Exceptions
                        }
                        finally
                        {
                            OraConn.Dispose();
                        }
                    }
                }
            }
            else
            {
                //MessageBox
            }
        }

//Fill 2nd ComboBoxColumn
private void fill_combobox2(int row_number)
        {
            datagridview_col2.Items.Clear();

            string rcs = db_conn.connection();

            using (var OraConn = new OracleConnection(rcs))
            {
                using (var OraCmd = OraConn.CreateCommand())
                {
                    try
                    {
                        OraConn.Open();
                        OraCmd.BindByName = true;

                        OraCmd.CommandText = "Oracle Command with id as a Parameter";

                        var id_param = new OracleParameter("id", id);
                        OraCmd.Parameters.Add(id_param);

                        OracleDataReader OraDataReader = OraCmd.ExecuteReader();
                        if (OraDataReader.Read() == false)
                        {
                            //MessageBox
                        }
                        else
                        {
                            using (var OraDat = new OracleDataAdapter(OraCmd))
                            {
                                using (var combo2_table = new DataTable())
                                {
                                    OraDat.Fill(combo2_table);

                                    foreach (DataRow row in combo2_table.Rows)
                                    {
                                        foreach (DataColumn column in combo2_table.Columns)
                                        {
                                            if (row[column] != null)
                                            {
                                                if (column.ColumnName == "BUILDING_LESS")
                                                {
                                                        datagridview_col2.Items.Add(row[column.ColumnName].ToString());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (OracleException ex)
                    {
//Catch Exceptions
                    }
                    finally
                    {
                        OraConn.Dispose();
                    }
                }
            }
        }


//Trigger SelectionChange
private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.SelectedIndexChanged -= new EventHandler(selectionchange);

                cb.SelectedIndexChanged += selectionchange;
            }
        }


        private void selectionchange(object sender, EventArgs e)
        {
            try
            {
                //ComboBox cb = (ComboBox)sender;
                String selected = (sender as ComboBox).SelectedItem.ToString();

                if (datagridview.CurrentCell.ColumnIndex == 0)
                {
                    row_number = datagridview.CurrentCell.RowIndex;
                    id = selected.Substring(0, 6);
                    if (!String.IsNullOrEmpty(id))
                    {
                        fill_combobox2(row_number);
                    }
                    else if (String.IsNullOrEmpty(id))
                    {
                        //MessageBox);
                    }
                }
                else if (datagridview.CurrentCell.ColumnIndex == 1)
                {
                    section = selected.Substring(0, 2);
                    if (!String.IsNullOrEmpty(id) && !String.IsNullOrEmpty(value2))
                    {
                        //MessageBox
                    }
                    else if (String.IsNullOrEmpty(id) && !String.IsNullOrEmpty(value2))
                    {
                        //MessageBox
                    }
                    else if (String.IsNullOrEmpty(id) && String.IsNullOrEmpty(value2))
                    {
                        //MessageBox
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox
            }
        }
Posted
Updated 2-Aug-20 23:38pm
v3
Comments
Garth J Lancaster 3-Aug-20 5:34am    
"it should only affect the ComboBox on the gray row" .. ok, but you wrote the code didn't you ? unfortunately, since you haven't shown us the code for the relevant click event/handler, there's not much we can do to help .. how about using Improve solution so we can see the code and outline potential issues ?
mayrhofer-simon 3-Aug-20 5:36am    
Now i Updated my Question

1 solution

Use the DataGridview.CellClick event[^] and check the column - it's index is passed in the DataGridViewCellEventArgs[^] along with the row index.
You can check if it's the checkbox column, and if so, check the checkbox value.
Then you use the RowIndex to update just that row.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900