简体   繁体   中英

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.

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;
               

            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM