none
Filtering calculated column in datatable

    Question

  • Hi ,

    If i have some calculated column with aggregate functions in some data table how can i  do that according to some filters ,Now i  want to calculate some data i should do filtering for all  data table as a whole but what need that is it possible to calculate while data table displaying all records

    Thanks

    dear Karen,

    this time i  am avoided considering Grammarly tool  correction so feel relax  ;)  :* :* :-*


    • Edited by Esmat_Halfa Tuesday, April 11, 2017 2:33 PM
    Tuesday, April 11, 2017 2:20 PM

All replies

  • By calculated do you mean using an Expression

    dt.Columns.Add(new DataColumn()
    {
        ColumnName = "Total",
        DataType = typeof(int),
        Expression = "Cost * Markup"
    });


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, April 11, 2017 6:53 PM
    Moderator
  • Hi Esmat_Halfa,

    Thank you for posting here.

    For your question, you could use DataColumn.Expression preperty.

    Here is a simple example for your reference.

        private void Form1_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
    
                // Create the first column.
                DataColumn priceColumn = new DataColumn();
                priceColumn.DataType = System.Type.GetType("System.Decimal");
                priceColumn.ColumnName = "price";
                priceColumn.DefaultValue = 50;
    
                // Create the second, calculated, column.
                DataColumn taxColumn = new DataColumn();
                taxColumn.DataType = System.Type.GetType("System.Decimal");
                taxColumn.ColumnName = "tax";
                taxColumn.Expression = "price * 0.0862";
    
                // Create third column.
                DataColumn totalColumn = new DataColumn();
                totalColumn.DataType = System.Type.GetType("System.Decimal");
                totalColumn.ColumnName = "total";
                totalColumn.Expression = "price + tax";
    
                // Add columns to DataTable.
                table.Columns.Add(priceColumn);
                table.Columns.Add(taxColumn);
                table.Columns.Add(totalColumn);
    
                DataRow row = table.NewRow();
                table.Rows.Add(row);
                DataView view = new DataView(table);
                dataGridView1.DataSource = view;
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by RohitArora Wednesday, April 12, 2017 4:44 AM
    Wednesday, April 12, 2017 3:17 AM
    Moderator