简体   繁体   中英

adding multiple items one by one in datagridview in c# window form

private void btnSave1_Click(object sender, EventArgs e)
{
   foreach (DataGridViewRow dgvr in ProductAddGridView.Rows)
    {
        dgvr.Cells[0].Value = productIDTextBox.Text;
        dgvr.Cells[1].Value = productTypeIDComboBox.Text;
        dgvr.Cells[2].Value = companyIDComboBox.Text;
        dgvr.Cells[3].Value = productPurchaseRateTextBox.Text;
        dgvr.Cells[4].Value = productQtyTextBox.Text;
        dgvr.Cells[5].Value = productDescriptionRichTextBox.Text;
        dgvr.Cells[6].Value = Convert.ToString(Convert.ToUInt32(productQtyTextBox.Text.Trim()) * Convert.ToDecimal(productPurchaseRateTextBox.Text));
     }
}

I am trying to add multiple products one by one in a datagridview before saving it to database. But whenever I add a row in datagridview second time, the first row gets replaced by the second row -- it only works for single row addition.

I am not exactly getting what are you looking for but if you want to add multiple row in to your datagrid view you will need to create a data row and then you can add that row in datagrid. You can refer this Link

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