user3430861
user3430861

Reputation: 182

Updating ComboBox Column in datagridview

I am populating my datagridview with the below code.

dgvPChannel.AutoGenerateColumns = true;
        dgvPChannel.DataSource = new PaymentsAccess().getAllComplianceAccounts().ToList();

I have created an extra column in the datagridview and populated this combobox column. I will now need to update my database when changing this combobox to an option on and 'Update' button. How can I update all my datagridview items with the combo box option chosen for each.

Upvotes: 0

Views: 1300

Answers (1)

Badiparmagi
Badiparmagi

Reputation: 1285

If you loop through each ComboBox value in your grid, you can update rows that checked. check this:

   private void btnUpdate_Click(object sender, EventArgs e)
    {
         
        foreach (DataGridViewRow row in yourdataGridView.Rows)
        {
            var comboValue = string.IsNullOrEmpty(row.Cells[ComboBoxColumnName.Index].Value.ToString()) ? "" : row.Cells[ComboBoxColumnName.Index].Value.ToString();
            if (some logic here to update)
            {
                //update your_table set field = value where id = row.Cells["fieldname"].Value;
               

            }
        }

    }

Upvotes: 1

Related Questions