I'm struggling to copy rows from one DataGridView to another. This may sound redundant but it's necessary for my Orders project. I have 3 DataGridViews on one form.
The first DataGridView gets populated from my database with the product and total available stock. The second DataGridView is sort of a basket...so, when an item is selected from the first DataGridView, the item gets added to the second DataGridView and there's an editable column in the second DataGridView in which the user can enter an amount they want of the product in question.
The third one is just for show. It displays the product and the remaining stock quantity (what there was minus the amount entered by the user).
My problem is this: My item adds fine from the 1st DataGridView to the second, but when I add more than one item and edit the quantities, it doesn't subtract from the correct product, so I'm now trying to copy the rows of my 2nd DataGridView to my 3rd one and try in baby steps, but that's not going too well. Here's what I've tried already:
Expand|Select|Wrap|Line Numbers
- private void copyDGVRows()
- {
- dgvProductsAfterOrders.Rows.Clear();
- for (int i = 0; i <= dgvLineItems.Rows.Count; i++)
- {
- dgvProductsAfterOrders.Rows[i].Cells[i].Value = dgvLineItems.Rows[i].Cells[i].Value;
- //dgvProductsAfterOrders.Rows.Add(i + 1);
- }
- }
- private void copyDGVRows()
- {
- dgvProductsAfterOrders.Rows.Clear();
- if (dgvLineItems.Rows.Count == 0)
- {
- return;
- }
- else
- {
- foreach (DataGridViewRow row in dgvLineItems.Rows)
- {
- row.Cells[1].Clone();
- for (int i = 0; i < row.Cells.Count; i++)
- {
- dgvProductsAfterOrders.Rows[i].Cells[1].Value = row;
- //dgvProductsAfterOrders.Rows.Add(row);
- }
- }
- }
- }
- private void copyDGVRows(DataGridView dataGridView, ArrayList rowsToCopy)
- {
- for (int i = 0; i < rowsToCopy.Count; i++)
- {
- DataGridViewRow row = new DataGridViewRow();
- row = (DataGridViewRow)dataGridView.Rows[Convert.ToInt32(rowsToCopy[i])].Clone();
- for (int j = 0; j < row.Cells.Count; j++)
- {
- row.Cells[j].Value = dataGridView.Rows[Convert.ToInt32(rowsToCopy[i])].Cells[j].Value;
- dataGridView.Rows.Add(row);
- }
- }
- }
- private void populate3rdGrid()
- {
- dgvProductsAfterOrders.Rows.Clear();
- for (int i = 0; i < dgvLineItems.Rows.Count; i++)
- {
- if (dgvProductsAfterOrders.Rows[i].Cells[0].Value != null)
- {
- dgvProductsAfterOrders.Rows.Add();
- for (int j = 0; j < dgvLineItems.Rows.Count; j++)
- {
- dgvProductsAfterOrders.Rows[i].Cells[j].Value = dgvLineItems.Rows[i].Cells[j].Value;
- }
- }
- }
- }