I have a DataGridView that contains 3 columns, the first is a drop down and the second and third are textboxes. However, the third one is read-only and is only there to provide information to the user on the item selected from the dropdown column.

If the user changes the drop-down, I need to update the text in the third column appropriately, but my problem is finding the appropriate event to handle this. I need the update to occur immediately, not after the row is validated, or after the row is committed. The information that the user enters in the second box is somewhat dependant on what the third box says.

Any suggestions?

Recommended Answers

All 3 Replies

Here are two options:

This one whould update after you leave the first column.

        private void dgv1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;


            if (sender is DataGridView && e.ColumnIndex == 0)
            {
                DataGridView dgv = (DataGridView)sender;

                if (dgv1.Equals(dgv))
                {
                    // set the value as needed
                    dgv.Rows[e.RowIndex].Cells[2].Value = "fred";

                }
            }
        }

This one updates immediately.

        private DataGridViewComboBoxEditingControl cboxEdit;
        private void EditComboBox_SelectionChangeCommitted(object sender, System.EventArgs e)
        {
            // set the value as needed
            dgv1.CurrentRow.Cells[2].Value = "wilma";
        }

        private void dgv1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgv1.CurrentCell.ColumnIndex == 0 && e.Control is DataGridViewComboBoxEditingControl)
            {
                if (cboxEdit != null)
                {
                    cboxEdit.SelectionChangeCommitted -= EditComboBox_SelectionChangeCommitted;
                }
                cboxEdit = (DataGridViewComboBoxEditingControl)e.Control;
                cboxEdit.SelectionChangeCommitted += EditComboBox_SelectionChangeCommitted;
            }
        }

Thanks for the input guys!
I think what I might do though is just pull the data out of the database with a dataset query. The information that goes into the 3rd box is stored in a SQL server (along with the drop down items) so I think I'll just use SQL and let the framework do the dirty work. Or at least try that first.

Thanks a ton! I really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.