locked
Count Max Value in DataTable(DataGrid) RRS feed

  • Question

  • How to count max value column in dataTable??
    C#;
    Wednesday, October 1, 2014 7:38 AM

Answers

  • if you want to get a maximum column number in a DataTable you can use below code:

     int maxValue;
            DataTable dtb = new DataTable();
    
            // some code here
    
            void M()
            {
                if (dtb.Columns.Count > 0)
                {
                    maxValue = dtb.Columns.Count;
                }
                else
                {
                    maxValue = 0;
                }
            }

    • Marked as answer by Caillen Tuesday, October 14, 2014 7:40 AM
    Wednesday, October 1, 2014 9:09 AM
  • If you want the total number of columns use:

    DataTable.Columns.Count();

    If you want the max value for a column you can use this:

      DataTable myDataTable = new DataTable();
                myDataTable.Columns.Add("amount");
                myDataTable.Columns["amount"].DataType=typeof(decimal);
                DataRow row1 = myDataTable.NewRow();
                row1["amount"] = 5.0;
                myDataTable.Rows.Add(row1);
                DataRow row2 = myDataTable.NewRow();
                row2["amount"] = 9.0;
                myDataTable.Rows.Add(row2);
    
                //int totalColumns = myDataTable.Columns.Count;
    
                
                decimal maxamount = (Decimal)myDataTable.AsEnumerable().Max(row => row["amount"]);
    


    "If there's nothing wrong with me, maybe there's something wrong with the universe!"

    • Marked as answer by Caillen Tuesday, October 14, 2014 7:40 AM
    Wednesday, October 1, 2014 9:10 AM

All replies

  • Can you give more details?  The question can be interpreted a few different ways.

    jdweng

    Wednesday, October 1, 2014 8:54 AM
  • if you want to get a maximum column number in a DataTable you can use below code:

     int maxValue;
            DataTable dtb = new DataTable();
    
            // some code here
    
            void M()
            {
                if (dtb.Columns.Count > 0)
                {
                    maxValue = dtb.Columns.Count;
                }
                else
                {
                    maxValue = 0;
                }
            }

    • Marked as answer by Caillen Tuesday, October 14, 2014 7:40 AM
    Wednesday, October 1, 2014 9:09 AM
  • If you want the total number of columns use:

    DataTable.Columns.Count();

    If you want the max value for a column you can use this:

      DataTable myDataTable = new DataTable();
                myDataTable.Columns.Add("amount");
                myDataTable.Columns["amount"].DataType=typeof(decimal);
                DataRow row1 = myDataTable.NewRow();
                row1["amount"] = 5.0;
                myDataTable.Rows.Add(row1);
                DataRow row2 = myDataTable.NewRow();
                row2["amount"] = 9.0;
                myDataTable.Rows.Add(row2);
    
                //int totalColumns = myDataTable.Columns.Count;
    
                
                decimal maxamount = (Decimal)myDataTable.AsEnumerable().Max(row => row["amount"]);
    


    "If there's nothing wrong with me, maybe there's something wrong with the universe!"

    • Marked as answer by Caillen Tuesday, October 14, 2014 7:40 AM
    Wednesday, October 1, 2014 9:10 AM