September 0803

Dynamically adding rows to DataGrid

I am working on a WinForms application for a Jeweller I know; there's a section of it that will allow him to keep track of the items he makes by entering what materials he uses and how much is wastage as well as the time spent on each item and any other services he may provide such as engraving.

To present this I am using a Datagrid in which each row represents an item or service with columns to enter the amount used and the 1st column is a ComboBox to let him pick the material or service, its cost per unit, the total as well as the wastage, cost per unit total and then line total. The costs per unit are stored in the database and so the totals are calculated when he enters the number of units used. But as each bespoke item of jewellery that he creates will have different combinations of materials I can't hardcode the rows, instead I need to allow him to dynamically add new rows to the DataGrid and each row can be either for a service or a material (such as gold, silver or precious stones).

Initially on page load I want to create 2 rows in the grid, 1 for hours (as every project will take some time) and the other for engraving - though this may change. So I have this method:

private void AddRows(int numRowsToAdd, bool staticList)
{
    for (int i = 0; i < numRowsToAdd; i++)
    {
        dataGridView1.Rows.Add();
    }
} 

But as I said the 1st column in each row is a DataGridComboBoxCell and I need to populate this with either the materials or services, so as each row is added I get a reference to the ComboBoxCell and then determine which type of ComboBox I actually want. In this case I'm just hardcoding that any thing more than the 2nd row should be a MaterialsDDL

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    DataGridViewComboBoxCell dgvCBC = dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewComboBoxCell;
    if (dataGridView1.RowCount > 2)
    {
        BindMaterialsDDL(dgvCBC);
    }
    else
    {
        BindServicesDDL(dgvCBC);
    }
}

Then when he's entering the units used calculating the new totals per row. Again there's some hardcoding going on here - but this was just a prototyping exercise to demonstrate to my client to be sure that I was on the right lines before investing a lot more time in developing it further

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (rowIndex != -1)
            {
                int columnIndex = e.ColumnIndex;
                CalculateCells(rowIndex, columnIndex);
                Decimal theTotal = 0M;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    DataGridViewCell totalCell = row.Cells[7];
                    Decimal currentTotal = totalCell.Value != null ? Decimal.Parse(totalCell.Value.ToString()) : 0M;
                    if (currentTotal > 0)
                    {
                        theTotal += Decimal.Parse(row.Cells[7].Value.ToString());
                    }
                }
                txtTotal.Text = theTotal.ToString();
            }
        }[/code]
private void CalculateCells(int rowIndex, int columnIndex)
        {
            DataGridViewCell itemCell = dataGridView1.Rows[rowIndex].Cells[0];
            DataGridViewCell usedCell = dataGridView1.Rows[rowIndex].Cells[1];
            DataGridViewCell costCell = dataGridView1.Rows[rowIndex].Cells[2];
            DataGridViewCell chargeCell = dataGridView1.Rows[rowIndex].Cells[3];
            DataGridViewCell wastageCell = dataGridView1.Rows[rowIndex].Cells[4];
            DataGridViewCell wastageCostCell = dataGridView1.Rows[rowIndex].Cells[5];
            DataGridViewCell wastageChargeCell = dataGridView1.Rows[rowIndex].Cells[6];
            DataGridViewCell totalCell = dataGridView1.Rows[rowIndex].Cells[7];
            int itemID = int.Parse(itemCell.Value.ToString());
            Decimal usedAmount = usedCell.Value != null ? Decimal.Parse(usedCell.Value.ToString()) : 0M;
            Decimal costAmount = costCell.Value != null ? Decimal.Parse(costCell.Value.ToString()) : 0M;
            Decimal chargeAmount = chargeCell.Value != null ? Decimal.Parse(chargeCell.Value.ToString()) : 0M;
            Decimal wastageAmount = wastageCell.Value != null ? Decimal.Parse(wastageCell.Value.ToString()) : 0M;
            Decimal wastageCostAmount = wastageCostCell.Value != null ? Decimal.Parse(wastageCostCell.Value.ToString()) : 0M;
            Decimal wastageChargeAmount = wastageChargeCell.Value != null ? Decimal.Parse(wastageChargeCell.Value.ToString()) : 0M;
            Decimal totalAmount = totalCell.Value != null ? Decimal.Parse(totalCell.Value.ToString()) : 0M;
            if (itemID == -1)
            {
                costCell.Value = "15";
            }
            if (itemID == -2)
            {
                costCell.Value = "20";
            }
            if (columnIndex == 1)
            {
                chargeCell.Value = usedAmount * costAmount;
            }
            totalCell.Value = chargeAmount +  wastageChargeAmount;
        } 
Permalink | Comments (0)
Comments are closed