Home Forums WinForms controls Xceed Grid for WinForms StatFunction using more than one column to calculate weighted average. Reply To: StatFunction using more than one column to calculate weighted average.

Diane [Xceed]
Moderator
Post count: 1353

Hi Comodo,

To achieve this, you will need the SiblingDataRowsChanged of the SummaryRow.

If you subscribe to that event and update a ValueRow or TextRow, you will get what you need.

For example:

  {
    m_summaryRow = new SummaryRow();
    grid.FixedFooterRows.Add( m_summaryRow );

    // Value1
    var summaryCell = m_summaryRow.Cells[ 0 ] as SummaryCell;
    summaryCell.StatFunction = StatFunction.Sum;

    // Value2
    summaryCell = m_summaryRow.Cells[ 1 ] as SummaryCell;
    summaryCell.StatFunction = StatFunction.Sum;

    m_textRow  = new TextRow();
    grid.FixedFooterRows.Add( m_textRow );

    this.RefreshTextRow();
    m_summaryRow.SiblingDataRowsChanged += ( s, args ) => this.RefreshTextRow();
  }

  private void RefreshTextRow()
  {
    // Custom Calculation using Value1 and Value2
    var stat = ( ( int )m_summaryRow.Cells[ 0 ].Value + ( int )m_summaryRow.Cells[ 1 ].Value ) / 2;
    m_textRow.Text = stat.ToString();
  }

If you need to put this value directly in the SummaryRow, then that is more complicated, as you cannot force a value in a SummaryCell, but you can set the TextFormat of the SummaryRow to display your information.

For example (using our SolidFoundation sample):

  private void InitializeGridControl()
  {
    […]

    var summaryCell = this.summaryRow1.Cells[0] as SummaryCell;
    summaryCell.StatFunction = StatFunction.Count;
    summaryCell.Visible = false;

    summaryCell = this.summaryRow1.Cells[3] as SummaryCell;
    summaryCell.StatFunction = StatFunction.Sum;
    summaryCell.Visible = false;

    this.RefreshSummaryRow();
    this.summaryRow1.SiblingDataRowsChanged += (s, args) => this.RefreshSummaryRow();

    this.gridControl1.EndInit();
  }

  private void RefreshSummaryRow()
  {
    double value1 = double.Parse(this.summaryRow1.Cells[3].Value.ToString());
    double value2 = double.Parse(this.summaryRow1.Cells[0].Value.ToString());

    double result = value1 / value2;

    this.summaryRow1.TextFormat = “Total UnitPrice / Record Count = ” + result.ToString();
  }

Or alternatively, display all the results in a ValueRow instead:

  private void InitializeGridControl()
  {
    […]

    var summaryCell = this.summaryRow1.Cells[0] as SummaryCell;
    summaryCell.StatFunction = StatFunction.Count;

    summaryCell = this.summaryRow1.Cells[3] as SummaryCell;
    summaryCell.StatFunction = StatFunction.Sum;

    m_valueRow = new ValueRow();
    this.gridControl1.FixedFooterRows.Add(m_valueRow);
    m_valueRow.Cells[2].CellViewerManager = new CellViewerManager();

    this.RefreshValueRow();
    this.summaryRow1.SiblingDataRowsChanged += (s, args) => this.RefreshValueRow();

    this.summaryRow1.Visible = false;
    this.gridControl1.EndInit();
  }

  ValueRow m_valueRow;
  private void RefreshValueRow()
  {
    double value1 = double.Parse(this.summaryRow1.Cells[3].Value.ToString());
    double value2 = double.Parse(this.summaryRow1.Cells[0].Value.ToString());
    double result = value1 / value2;

    m_valueRow.Cells[3].Value = value1.ToString();
    m_valueRow.Cells[0].Value = value2.ToString();
    m_valueRow.Cells[2].Value = result.ToString();
  }

Imported from legacy forums. Posted by Diane [Xceed] (had 233 views)