How to get the dynamic dynamics of a datagridview column in a text box

I want to get the sum of a datagridview column and show this in a text box. After each entry, the amount must be changed dynamically. For this, I use the textChanged event of the text field, but when the recording is complete, it does not show any result. I want to get the result dynamically in a text box. I want to avoid using a button for the amount.

Here is the code snippet for the textChanged text event:

private void textBox9_TextChanged(object sender, EventArgs e)
        {
            double sum = 0;
            for (int i = 0; i < dataGridView2.Rows.Count; ++i)
            {
                sum += Convert.ToDouble(dataGridView2.Rows[i].Cells[5].Value);
            }
            textBox9.Text = sum.ToString();
        }
+4
source share
5 answers

, CellValueChanged , , , . , , .

    private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 5)
            textBox9.Text = CellSum().ToString();
    }

    private double CellSum()
    {
        double sum = 0;
        for (int i = 0; i < dataGridView2.Rows.Count; ++i)
        {
            double d = 0;
            Double.TryParse(dataGridView2.Rows[i].Cells[5].Value.ToString(), out d);
            sum += d;
        }
        return sum;
    }

, - , . , textbox.Text, ...

Double.TryParse, , . , ( / ?), , .

, , . CellValueChanged CellEndEdit, - , , .

+9

:

private void your_name()
{

    Double your_variable = dataGridView1.Rows.Cast<DataGridViewRow>().Sum(x => Convert.ToDouble(x.Cells["Column4"].Value));

    this.textBox1.Text = your_variable.ToString("N2");
}
+3

, , CellEndEdit.

, , . ( 0, , , - .)

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 5)
    {
        decimal result;
        if (!Decimal.TryParse(Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value), out result))
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;

        textBox1.Text = dataGridView1.Rows.Cast<DataGridViewRow>()
                                     .Sum(x => Convert.ToDecimal(x.Cells[5].Value))
                                     .ToString();
    }
}

... .

+2

CellValueChanged. , , , , . , . . 1 .

//This is used as CellTemplate for your interested column
public class TrackedValueDataGridViewCell : DataGridViewTextBoxCell {
    public object OldValue { get; set; }
    protected override bool SetValue(int rowIndex, object value) {
      OldValue = Value;                
      return base.SetValue(rowIndex, value);
    }
}

public partial class Form1 : Form {
  public Form1(){
    InitializeComponent();
    //init your grid
    dataGridView1.DataSource = yourDataSource;
    dataGridView1.Columns["sumColumn"].CellTemplate = new TrackedValueDataGridViewCell();
    sum = InitSum(dataGridView1,"sumColumn");
    textBox9.Text = sum.ToString();
    dataGridView1.CellValueChanged += (s,e) => {
      if(dataGridView1.Columns[e.ColumnIndex].Name != "sumColumn") return;
      var cell = ((TrackedValueDataGridViewCell) dataGridView1[e.ColumnIndex, e.RowIndex]);
      sum -= ((double?) cell.OldValue).GetValueOrDefault();
      sum += ((double?)cell.Value).GetValueOrDefault();
      textBox9.Text = sum.ToString();
    };

  }
  double sum;
  public double InitSum(DataGridView grid, string colName) {
      return grid.Rows.OfType<DataGridViewRow>()                       
                 .Sum(row => ((double?) row.Cells[colName].Value).GetValueOrDefault());
  }
}

. , , , sumColumn, Name , . . , , , sum . RowsAdded. Form.Load:

dataGridView1.RowsAdded += (s, e) => {
    for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++) {
        sum += ((double?)dataGridView1["sumColumn", i].Value).GetValueOrDefault();
    }
    textBox9.Text = sum.ToString();
};

. RowsRemoved , RowsRemoved, , , :

dataGridView1.RowsRemoved += (s, e) => {
  sum = InitSum(dataGridView1,"sumColumn");
  textBox9.Text = sum.ToString();      
};

, . , , UserDeletingRow ( , ). , , , sum, - , :

//for user removing rows
dataGridView1.UserDeletingRow += (s, e) => {
  sum -= ((double?) e.Row.Cells["sumColumn"].Value).GetValueOrDefault();
  textBox9.Text = sum.ToString();
};
//for code removing rows, use some method to remove rows like this:
public void RemoveRows(DataGridView grid, int startIndex, int count){
  for(int i = startIndex; i <= startIndex + count; i++){
     //update the sum first
     sum -= ((double?)grid.Rows[i].Cells["sumColumn"].Value).GetValueOrDefault();
     //then remove the row
     grid.Rows.RemoveAt(startIndex);
  }
  textBox9.Text = sum.ToString();
}

Please note that all event handlers must be registered after , your grid has been initialized using some initial data following initialization sum.

+1
source
private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 5)
        {
            summition();
        }
    }
    void summition() 
    { 
        double sum = 0;
        foreach (DataGridViewRow row in dataGridView2.Rows)
        {
            if(!row .IsNewRow )
                sum += Convert.ToDouble(row.Cells [5].Value .ToString () );
        }


        textBox9.Text = sum.ToString();
    }
+1
source

All Articles