0

I am developing windows form using c# and using datagridview object. I am almost done but I have a problem with displaying item value to a specific column(PTS GAIN COLUMN) that I selected in a comboboxcell all inside datagridview. Data is selected from database(coded). The column(PTS GAIN COLUMN) where I want to display the selected item value has no entry in the database. It is empty. I want that every time I select a item from a comboboxcell per row is that it will display the value to a specific column(PTS GAIN COLUMN) and compute the total dynamically/real-time(I want to show the result in label.text)

Also the combobox cell has items YES,NO,NA(this has no datatable, I just added the items by coding combobox.items.add("yes/no/na"). Yes item will get value depending on the column PTS AVAIL and display on column PTS GAIN. If I select no, 0 will display in PTS GAIN column, and if NA, both PTS AVAIL and PTS GAIN will have 0. Again I want to if possible to compute the total real-time.

Any help with this matter is much appreciated. I am meeting a dead line so please, anyone! Have a great day! Btw, I will post screenshot of the program, and if you want to see a particular block of code for reference just comment.

enter image description here

Micho
  • 3,929
  • 13
  • 37
  • 40
  • Have you considered checking when a combo boxes value is changed and do what you describe at that time? – JohnG Jun 30 '17 at 01:29
  • It is my first time to code and I find datagridview object to be complicated. Do you want me to show you some block of code? @JohnG – theycallmesteeezy_ Jun 30 '17 at 01:33
  • Do not have time for that... try looking up the datagridviews, `CellValueChanged` event. Try something and if it doesn't work... post what you have tried. – JohnG Jun 30 '17 at 01:36

1 Answers1

0

You will need to hook up with 2 events to accomplish this but for the most part it is pretty easy.

private void MyInitializeComponent()
{
    dg.CurrentCellDirtyStateChanged += Dg_CurrentCellDirtyStateChanged;
    dg.CellValueChanged += Dg_CellValueChanged;

    this.CalculateTotals();
}

private void Dg_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dg.Columns[e.ColumnIndex].Name == "Choices")
    {
        switch (dg.Rows[e.RowIndex].Cells["Choices"].Value.ToString())
        {
            case ("Yes"):
            {
                dg.Rows[e.RowIndex].Cells["PointsGained"].Value =
                    dg.Rows[dg.CurrentCell.RowIndex].Cells["PointsAvailable"].Value;
                break;
            }
            case ("No"):
            {
                dg.Rows[e.RowIndex].Cells["PointsGained"].Value = 0;
                break;
            }
            case ("NA"):
            {
                dg.Rows[e.RowIndex].Cells["PointsGained"].Value = "NA";
                break;
            }
        }

        this.CalculateTotals();
    }
}

private void Dg_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if ((dg.Columns[dg.CurrentCell.ColumnIndex].Name == "Choices") &&
        (dg.IsCurrentCellDirty))
    {
        dg.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

private void CalculateTotals()
{
    var totalPointsGained = dg.Rows.Cast<DataGridViewRow>()
        .Where(a => a.Cells["PointsGained"].Value?.ToString() != "NA")
        .Sum(a => Convert.ToInt32(a.Cells["PointsGained"].Value));

    var totalPointsAvailable = dg.Rows.Cast<DataGridViewRow>()
        .Where(a => a.Cells["PointsAvailable"].Value?.ToString() != "NA")
        .Sum(a => Convert.ToInt32(a.Cells["PointsAvailable"].Value));

    lblTotalPointsGained.Text = "Total Points Gained: " + totalPointsGained;
    lblTotalAvailable.Text = "Total Points Available: " + totalPointsAvailable;
}

You can put the code I have in MyInitializeComponent() wherever you initialize your other objects on the form.

jaredbaszler
  • 3,941
  • 2
  • 32
  • 40