• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

How do I change the value for a specific row in a datagridview control if the user selects a value from a combobox within the datagridview

I have a VS 2010 C# Windows application, with a datagridview control on the form. Everytime the user selects a different item from the "Task" combobox in the datagridview control, I want the "BudgetedHours" value to change for that row. How would I go this?

Below is the code I have so far.

private void AddComboBoxColumns()
        {
            DataGridViewComboBoxColumn comboboxColumn;
            comboboxColumn = CreateComboBoxColumn();
            SetAlternateChoicesUsingDataSource(comboboxColumn);
            comboboxColumn.HeaderText = "Task";

            //comboboxColumn.ValueMember = "TaskID";
            dgTime.Columns.Insert(2, comboboxColumn);
            dgTime.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgTime_EditingControlShowing);
        }

        private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
        {
            OperationsDataContext dc = new OperationsDataContext();
            {
                comboboxColumn.DataSource = dc.Tasks_GetByProjectID(Convert.ToInt32(cboTimeProject.SelectedValue));
                comboboxColumn.ValueMember = "TaskID";
                comboboxColumn.DisplayMember = "TaskDescription";
            }
        }

        private void dgTime_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
                // Remove an existing event-handler, if present, to avoid 
                // adding multiple handlers when the editing control is reused.
                combo.SelectedIndexChanged -=
                    new EventHandler(ComboBox_SelectedIndexChanged);

                // Add the event handler. 
                combo.SelectedIndexChanged +=
                    new EventHandler(ComboBox_SelectedIndexChanged);
            }
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
            using (OperationsDataContext dc = new OperationsDataContext())
            {
                //dc.Tasks.InsertOnSubmit(del);
                //dc.SubmitChanges();

                //MessageBox.Show("Deliverable successfully saved.", "Deliverable", MessageBoxButtons.OK);
                var hours = dc.Tasks_GetByTaskID(Convert.ToInt32(((ComboBox)sender).SelectedValue));

                DataGridViewColumn col = (DataGridViewColumn)dgTime.Columns["BudgetedHours"];
                foreach (var hour in hours)
                {
                    col.DataPropertyName = Convert.ToString(hour.OriginalBudget);
                }
            }

        }

        private DataGridViewComboBoxColumn CreateComboBoxColumn()
        {
            DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
            {
                //if (!isNewRow)
                //{
                column.DataPropertyName = "TaskID";
                //}
                column.HeaderText = "Task";
                column.DropDownWidth = 160;
                column.Width = 160;
                column.MaxDropDownItems = 3;
                column.FlatStyle = FlatStyle.Flat;
            }
            return column;
        }

Open in new window

DatagridviewControl.png
0
jjliu4492
Asked:
jjliu4492
1 Solution
 
djon2003Commented:
There is a problem in your ComboBox_Selected method. You are changing a property related to a column, which is not the data at all. DataPropertyName sets the name of the property from the table of the datasource linked. What you have to change is the data of the cell. Which can be achieved by DGVC.Rows(#).Cells(#).Value = NEWVALUE.
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
            using (OperationsDataContext dc = new OperationsDataContext())
            {
                //dc.Tasks.InsertOnSubmit(del);
                //dc.SubmitChanges();

                //MessageBox.Show("Deliverable successfully saved.", "Deliverable", MessageBoxButtons.OK);
                var hours = dc.Tasks_GetByTaskID(Convert.ToInt32(((ComboBox)sender).SelectedValue));
                dgTime.Rows[dgTime.CurrentRow.Index].Cells["BudgetedHours"].Value =  Convert.ToString(hour.OriginalBudget);


        }

Open in new window

0
 
jjliu4492Author Commented:
Awesome thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now