public static void DataGridApplyExpressionMenuItem(object sender, DBTableControl.DBEditorTableControl mainTable) { ApplyExpressionWindow getexpwindow = new ApplyExpressionWindow(); getexpwindow.ShowDialog(); if (getexpwindow.DialogResult != null && (bool)getexpwindow.DialogResult) { foreach (DataGridCellInfo cellinfo in mainTable.dbDataGrid.SelectedCells) { // Determine current cells indecies, row and column int columnindex = mainTable.CurrentTable.Columns.IndexOf((string)cellinfo.Column.Header); int rowindex = mainTable.CurrentTable.Rows.IndexOf((cellinfo.Item as DataRowView).Row); // Skip any filtered rows, or any rows with an error in the column we are computing. mainTable.UpdateVisibleRows(); if (mainTable._visibleRows[rowindex] == System.Windows.Visibility.Collapsed || mainTable._errorList.Count(n => n.RowIndex == rowindex && n.ColumnName.Equals((string)cellinfo.Column.Header)) > 0) { continue; } // Get the expression, replacing x for the current cell's value. string expression = getexpwindow.EnteredExpression.Replace("x", string.Format("{0}", mainTable.CurrentTable.Rows[rowindex][columnindex])); // Compute spits out the new value after the current value is applied to the expression given. object newvalue = mainTable.CurrentTable.Compute(expression, ""); // For integer based columns, do a round first if necessary. if (mainTable.EditedFile.CurrentType.Fields[columnindex].TypeCode == TypeCode.Int32 || mainTable.EditedFile.CurrentType.Fields[columnindex].TypeCode == TypeCode.Int16) { int newintvalue; if (!Int32.TryParse(newvalue.ToString(), out newintvalue)) { double tempvalue = Double.Parse(newvalue.ToString()); tempvalue = Math.Round(tempvalue, 0); newintvalue = (int)tempvalue; } newvalue = newintvalue; } mainTable.CurrentTable.Rows[rowindex][columnindex] = newvalue; // Refresh the cell in the UI, using its visual coordinates. mainTable.RefreshCell(mainTable.dbDataGrid.Items.IndexOf(cellinfo.Item), mainTable.dbDataGrid.Columns.IndexOf(cellinfo.Column)); } } }
public static void ColumnApplyExpression(object sender, DBTableControl.DBEditorTableControl mainTable) { DataGridColumn col = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget as DataGridColumnHeader).Column; string colname = (string)col.Header; ApplyExpressionWindow getexpwindow = new ApplyExpressionWindow(); getexpwindow.ShowDialog(); if (getexpwindow.DialogResult != null && (bool)getexpwindow.DialogResult) { for (int i = 0; i < mainTable.CurrentTable.Rows.Count; i++) { // Skip any filtered rows, or any rows with an error in the column we are computing. mainTable.UpdateVisibleRows(); if (mainTable._visibleRows[i] == System.Windows.Visibility.Collapsed || mainTable._errorList.Count(n => n.RowIndex == i && n.ColumnName.Equals(colname)) > 0) { continue; } // Grab the given expression, modifying it for each cell. string expression = getexpwindow.EnteredExpression.Replace("x", string.Format("{0}", mainTable.CurrentTable.Rows[i][colname])); object newvalue = mainTable.CurrentTable.Compute(expression, ""); int colindex = mainTable.CurrentTable.Columns.IndexOf(colname); // For integer based columns, do a round first if necessary. if (mainTable.EditedFile.CurrentType.Fields[colindex].TypeCode == TypeCode.Int32 || mainTable.EditedFile.CurrentType.Fields[colindex].TypeCode == TypeCode.Int16) { int newintvalue; if (!Int32.TryParse(newvalue.ToString(), out newintvalue)) { double tempvalue = Double.Parse(newvalue.ToString()); tempvalue = Math.Round(tempvalue, 0); newintvalue = (int)tempvalue; } newvalue = newintvalue; } mainTable.CurrentTable.Rows[i][colname] = newvalue; } } mainTable.RefreshColumn(mainTable.dbDataGrid.Columns.IndexOf(col)); }