How to select maximum vaue in datatable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandipkumar
    New Member
    • Jan 2011
    • 2

    How to select maximum vaue in datatable?

    How to Select maximum value in datatable?
  • Samuel Jones
    New Member
    • Jan 2011
    • 48

    #2
    Using a Google search I found some code at http://www.devx.com/codemag/Article/35186/1763/page/4 and modified it.

    Add this code to your function.
    It will search a specified column to find its max value and select its cell.

    Code:
    //Call using 'FindMaxValue(DataGridView1, 0);'
    
    public long MaxValue;
    
    public void FindMaxValue(DataGridView dgv, int colIndex)
       {
          MaxValue = 0;
          for (int rowIndex = 0;
             rowIndex < dgv.Rows.Count;
             rowIndex++)
          {
             DataGridViewRow row =
                dgv.Rows[rowIndex];
             if (MaxValue != Math.Max(MaxValue,
                Convert.ToInt64(row.Cells[colIndex].Value)))
             {
                dgv.row.Cells[colIndex].Selected = true;
                MaxValue = Convert.ToInt64(row.Cells[colIndex].Value);
             }
          }
       }
    Hope this helps.

    Sam

    Comment

    • Rohit Garg
      New Member
      • Jul 2010
      • 8

      #3
      You can use Select Method of DataTable class.
      Have a look at this http://msdn.microsoft.com/en-us/library/way3dy9w.aspx

      Comment

      Working...