-1

I am working on examination result system in vb.net which requires to calculate student ranks based on marks obtained. Subject marks data is stored in database. I am loading the subject marks in a datatable

da.Fill(dt) 'added to a datagridview.  
DataGridView1.DataSource = dt 

then Add New columns in dt to show result:

            dt.Columns.Add("Obtained Marks", GetType(String))
            dt.Columns.Add("Percent", GetType(String))
            dt.Columns.Add("Result", GetType(String))
            dt.Columns.Add("Rank", GetType(Integer))

Then calculated total of all the subjects & added in obtained marks columns by looping through rows & columns of datatable.

        For s As Integer = 0 To dt.Rows.Count - 1
            For t As Integer = 0 To dt.Columns.Count - 1
              obtmarks += CDbl(dt.Rows(s).Item(t))
            Next
         dt.Rows(s)("Obtained Marks") = obtmarks
         dt.Rows(s)("Result") = "PASS"
         dt.Rows(s)("Rank") = 'RANK OF STUDENT
       Next

How can i calculate rank/position of students on the basis of total marks contained in datatable column "Obtained Marks". i.e. Student with marks 436 Rank should be 1 Student with marks 429.5 Rank should be 2 Student with marks 412 Rank should be 3 ....

so on until all the rows in record. (Image atttached)

if there is any function for datatable which can help here or how can i add the logic in the loop to calculate rank of students and add the value in rank column. Thanks

P.S. I dnt want to sort the rows on obtained marks, but want to Add rank of each student in front of his/her marks, which is already order by their Roll No. enter image description here

Vehlad
  • 155
  • 2
  • 10
  • 20
  • You can sort on a DataView. I mean the sort happens on a object that doesn't affect the current order of the grid. – Steve Oct 03 '15 at 10:25
  • yes i know about datagridview sort, Already mentioned that i want to keep the visible rows as it is that is essential because students are displayed in order of their "Roll No" in that course. Have to fill the values of their rank in Rank column. – Vehlad Oct 03 '15 at 10:31
  • I am not talking of sorting the DataGridView. I think you could build a new object called DataView from your DataTable ordered by obtained marks then set the value of Rank to your table looking at the order of this DataView. – Steve Oct 03 '15 at 10:36
  • Oh thanks i will try this and reply. – Vehlad Oct 03 '15 at 10:38

1 Answers1

1

You could use this code to set the Rank column in your table

DataView dv = new DataView(dt, "", "ObtainedMarks desc", DataViewRowState.CurrentRows);
for(int x = 0; x < dv.Count; x++)
    dv[x].Row["Rank"] = x+1;

This could be done only after you have completed the code that calculates the column ObtainedMarks

Also, I suggest to execute all before setting the DataSource of the DataGridView to avoid unnecessary delays in the grid repainting itself when you have not yet finished with it

EDIT
To have the same rank for persons with the same marks you could try something like this

int lastMark = -1;
int currentRank = 0;
int atSameRank = 1;
DataView dv = new DataView(dt, "", "ObtainedMarks desc", DataViewRowState.CurrentRows);
for(int x = 0; x < dv.Count; x++)
{
    int currentMark = Convert.ToInt32(dv["ObtainedMarks"]);
    if(currentMark != lastMark)
    {
        lastMark = currentMark;
        currentRank = currentRank + atSameRank;
        atSameRank = 0;
    }
    else
        atSameRank++;
    dv[x].Row["Rank"] = currentRank;
}

WARNING, I am not at a PC where I could test it.

Steve
  • 213,761
  • 22
  • 232
  • 286