Hi ryutenkan,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView runat="server" ID="GridView1" />
Namespaces
C#
using System.Data;
using System.Linq;
VB.Net
Imports System.Data
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Ranking");
dt.Columns.Add("Name");
dt.Columns.Add("Points");
dt.Rows.Add("", "Alpha ", "10");
dt.Rows.Add("", "Beta", "10");
dt.Rows.Add("", "Gamma", "8");
dt.Rows.Add("", "Zeta", "9");
List<IGrouping<object, DataRow>> result = dt.AsEnumerable().GroupBy(x => x["Points"]).ToList();
int i = 1;
int j = 1;
foreach (IGrouping<object, DataRow> items in result)
{
foreach (DataRow dr in items.ToList())
{
dt.Select().Where(x => x["Name"] == dr["Name"]).FirstOrDefault()["Ranking"] = i;
j++;
}
i = j;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Ranking")
dt.Columns.Add("Name")
dt.Columns.Add("Points")
dt.Rows.Add("", "Alpha ", "10")
dt.Rows.Add("", "Beta", "10")
dt.Rows.Add("", "Gamma", "8")
dt.Rows.Add("", "Zeta", "9")
Dim result = dt.AsEnumerable().GroupBy(Function(x) x("Points"))
Dim i As Integer = 1
Dim j As Integer = 1
For Each items As IGrouping(Of Object, DataRow) In result
For Each dr As DataRow In items.ToList()
dt.Select().Where(Function(x) x("Name") = dr("Name")).FirstOrDefault()("Ranking") = i
j += 1
Next
i = j
Next
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Output
Ranking | Name | Points |
1 |
Alpha |
10 |
1 |
Beta |
10 |
3 |
Gamma |
8 |
4 |
Zeta |
9 |