Array ICalcData Demo

This sample adds calculation support to a double array by wrapping the array in a class that implements the ICalcData interface. The sample adds an extra row and column to the wrapper class to hold the calculation results. Here, an array of random doubles is generated. Then this array is wrapped in an ICalcData object that adds an extra row and column to hold the row sum and column sum of the original array.

Features:

The following illustration demonstrates the usage of the Generate Data button, that has been pressed to generate a 7x3 array of doubles. The multiline text box displays the array. The column on the right-hand side edge is the sum of the columns in the array, and the row at the very bottom is the sum of rows in the array.

Adding Calculation Support to Double Array

A 3x7 Array with Summary Rows and Columns


The following illustration displays the results of setting the value in row 1, column 1 to 1,000. Notice that the sum has been updated to reflect the newly entered value. The ICalcData interface handles the adjustments of the the row and column sum automatically.

Adding Calculation Support to a Double Array

After Setting a Cell Value of 1,000


The following code sets a single value in a data array and displays the results in the multiline text box.

		private void button2_Click(object sender, System.EventArgs e)
		{
			if (this.nRows == 0)
			{
				MessageBox.Show("Generate data first.");
				return;
			}

			int row = int.Parse(this.textBox2.Text);
			int col = int.Parse(this.textBox3.Text);
			string val = this.textBox4.Text;

			this.data[row, col] = val;

			ShowObject();
		}

		private void ShowObject()
		{
			this.textBox1.Text = "";
			for(int i = 0; i <= this.nRows; ++i)
			{
				for(int j = 0; j <= this.nCols; ++j)
				{
					this.textBox1.Text += this.data[i, j].ToString() + "\t";

				}
				this.textBox1.Text += "\r\n";
			}
		}