0

I've tried to compute a column in my datagrid, i will be showing these in my code as per below. I Keep getting these error.

I've go through these links
1. How To Convert The DataTable Column type?
2. Error while taking SUM() of column in datatable
3. Invalid usage of aggregate function Sum() and Type: String

//This is the column i add into my datagrid
MITTRA.Columns.Add("Min_Tol");
MITTRA.Columns.Add("Max_Tol");
MITTRA.Columns.Add("Min_Weight");
MITTRA.Columns.Add("Max_Weight");

// The following codes is working finely
// if you notice MTTRQT column, it's a data queried from database
for (int i = 0; i <= MITTRA.Rows.Count - 1; i++)
{
    string item = MITTRA.Rows[i]["MTITNO"].ToString();

    Tolerancechecking = database_select4.LoadUser_Tolerance(item);

    MITTRA.Rows[i]["Min_Tol"] = Tolerancechecking.Rows[0]["Min_Tol"].ToString();
    MITTRA.Rows[i]["Max_Tol"] = Tolerancechecking.Rows[0]["Max_Tol"].ToString();

    MITTRA.Rows[i]["Min_Weight"] = Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) - ((Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) * Convert.ToDecimal(MITTRA.Rows[i]["Min_Tol"]) / 10));
    MITTRA.Rows[i]["Max_Weight"] = Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) + ((Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) * Convert.ToDecimal(MITTRA.Rows[i]["Max_Tol"]) / 10));

    dataGrid2.Columns.Clear();
    dataGrid2.ItemsSource = null;
    dataGrid2.ItemsSource = Tolerancechecking.DefaultView;

}


//Working Sum computation
Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(MTTRQT)", string.Empty));  
MaxTol.Text = Sum.ToString();  /*** This is working as i got my value on the text box ****/

Errors when trying sum computation for different column Initial try

  1. Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Min_Weight)", string.Empty));

Errors occurred

Invalid usage of aggregate function Sum() and Type: String.

Second Attempts

  2.  Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Convert(Min_Weight,'System.Decimal'))", string.Empty));
  3.  Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Convert(Min_Weight,'System.Decimal'))", ""));

Errors occurred

Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.

How am i going to get the compute sum function to work?

Nian
  • 171
  • 1
  • 10
  • you have a typo in the last expressions: you meant to write `MITTRA.Compute("SUM(Convert(Min_Weight,'System.Decimal'))", "")` instead of `MITTRA.Compute("SUM(Convert(Min_Weight),'System.Decimal')", "")` – Bizhan May 05 '19 at 14:38
  • I will try it later and will update you, thanks Bizhan – Nian May 05 '19 at 15:02
  • @Bizhan thanks for correcting, but i still get the possible 'Child' qualifier error. – Nian May 05 '19 at 22:50
  • 1
    Try using decimal in constants such as 10, could be written as 10.000 or 10.000M. You also should define the column in the correct format instead of having all these conversions like so:DataColumn colDecimal = new DataColumn("DecimalCol"); colDecimal.DataType = System.Type.GetType("System.Decimal"); myTable.Columns.Add(colDecimal); – NoChance May 05 '19 at 23:02
  • @NoChance Alright, i will try that in a short while and will keep you updated. Thanks – Nian May 05 '19 at 23:05
  • @NoChance, Thank you, i've posted the answers and credited you as well, Thank you so much once again. – Nian May 06 '19 at 01:34
  • Glad your problem is solved. – NoChance May 06 '19 at 01:39

1 Answers1

2

Thank you @NoChance for the solutions. Hope this post can help others too.

/***** This is the part where i declare my datacolumn data type ****/
DataColumn Min_Weight = new DataColumn("Min_Weight");
Min_Weight.DataType = System.Type.GetType("System.Decimal");
MITTRA.Columns.Add(Min_Weight);

/**** This Works finally *****/ 
Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Min_Weight)", string.Empty)); 
MaxTol.Text = Sum.ToString(); 
Nian
  • 171
  • 1
  • 10
  • Usually when you post your own solution, you mark it as a "solution", so others know you are happy and don't try to solve the problem again. – NoChance May 06 '19 at 01:40
  • @NoChance Yeah, i know this, but i can't mark it yet, there's a 2 days time buffer before i can accept it as a "solution"( the Green tick ), correct me if i am wrong. – Nian May 06 '19 at 02:17