views:

143

answers:

1

Hi All,

I have specific requirement with datatable which i am not able to get. Please help me to solve my problem. My query is as below

  1. The datatable i have with data is
       C1  C2  C3  C4  C5
R1    1   2   3   4   25
R2    6   7   8   24  10
R3    11  22  13  14  15
R4    16  17  23  19  20
R5    21  7   18  9   5
  1. In output i would like to have max value from each colum with position (index) of row and colum as following
Value  Row Column
21    R5  C1
22    R3  C2
23    R4  C3
24    R2  C4
25    R1  C5

Can anyone please help me, how should i achieve result?

A: 

Assuming column names are C1, C2, C3, C4, C5.

Pseudo code/Algorithm.

int C1_MaxValue, C2_MaxValue, C3_MaxValue, C4_MaxValue, C5_MaxValue;
string C1_Row, C2_Row, C3_Row, C4_Row, C5_Row;

//Initialize row1 values as max values.
C1_MaxValue = DataTable.Rows[1]["C1"];
C2_MaxValue = DataTable.Rows[1]["C2"];
C3_MaxValue = DataTable.Rows[1]["C3"];
C4_MaxValue = DataTable.Rows[1]["C4"];
C5_MaxValue = DataTable.Rows[1]["C5"];

//Set R1 as row for max values.
C1_Row = C2_Row = C3_Row = C4_Row = C5_Row = DataTable.Rows[1][Row1];

For (i=2 to RowCount -1)

 if(Datatable.Rows[i]["C1"] > C1_MaxValue) 
 {       
      C1_MaxValue = Datatable.Rows[i][C1]; 
      C1_Row = Datatable.Rows[i][0];
 }
 //Do the above for all other columns.
End For

At the end of this for the above variables should contain the max values and corresponding max rows.

Aseem Gautam