Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am tring to set an auto numbered rank to a table so as the top scorer will be ranked 1st and so on. I have got a DataTable but it is not ordered by points as is in the GridView.
there is a way to order a DataTable so I can set auto rank? or by different way?

Here is what I tried:
Rank.asx.cs
C#
protected void Page_Load(object sender, EventArgs e)
{
    MyAdoHelper.SetRanks("db.mdf", "select player from ranking");
}

MyAdoHelper.cs
C#
public static void SetRanks(string fileName, string sql)
     {

        DataTable dt = ExecuteDataTable(fileName, sql);//gives the DataTable
        int rank = 1;
        string player = "";
        foreach (DataRow row in dt.Rows)
        {
            foreach (object myItemArray in row.ItemArray)
            {
                player = myItemArray.ToString();
                string sqlrank="update ranking set rank='"+rank+"' where player='"+player+"'";
                MyAdoHelper.DoQuery("db.mdf", sqlrank);
                rank++;
            }
        }
    }

Any ideas?

Thanks in advance :D
Posted
Updated 3-Jun-11 5:19am
v2
Comments
Sandeep Mewara 3-Jun-11 11:19am    
Formatting the code part by PRE tag makes the question readable. Please use it from next time.
OriginalGriff 3-Jun-11 12:17pm    
Answer updated

Construct a DataView, and give it a column name to sort on:
C#
public static void SetRanks(string fileName, string sql)
     {

        DataTable dt = ExecuteDataTable(fileName, sql);//gives the DataTable
        int rank = 1;
        string player = "";
        DataView dv = new DataView(dt, "", "rank", DataViewRowState.CurrentRows);
        foreach (DataRowView dr in dv)
        {
            DataRow row = dr.Row;
            foreach (object myItemArray in row.ItemArray)
            {
                player = myItemArray.ToString();
                string sqlrank="update ranking set rank='"+rank+"' where player='"+player+"'";
                MyAdoHelper.DoQuery("db.mdf", sqlrank);
                rank++;
            }
        }
    }


[edit + edit2]For some weird reason, the code disappeared when I pressed submit... - OriginalGriff[/edit + edit2]
 
Share this answer
 
v3
Thank you for that nice solution!
(here is the final version)

Rank.aspx.cs
C#
protected void Page_Load(object sender, EventArgs e)
{
    MyAdoHelper.SetRanks("db.mdf", "select player, allPoints from ranking");
}


MyAdoHelper.cs
    public static void SetRanks(string fileName, string sql)    
{
        DataTable dt = ExecuteDataTable(fileName, sql);//gives the DataTable
        int rank = 1;
        string player = "";
        DataView dv = new DataView(dt, "","allPoints DESC", DataViewRowState.CurrentRows);
        foreach (DataRowView dr in dv)
        {
            DataRow row = dr.Row;
            int num = 1;
            foreach (object myItemArray in row.ItemArray)
            {
                if (num == 1)//if it checks the 'player' field
                {
                    player = myItemArray.ToString();
                    string sqlrank = "update ranking set rank='" + rank + "' where player='" + player + "'";
                    MyAdoHelper.DoQuery("db.mdf", sqlrank);
                    rank++;
                    num++;
                }
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900