Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<hr />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
Code
C#
private const int BYE = -1;
// Return an array where results(i, j) gives
// the opponent of team i in round j.
// Note: num_teams must be odd.
private int[,] GenerateRoundRobinOdd(int num_teams)
{
int n2 = (int)((num_teams - 1) / 2);
int[,] results = new int[num_teams, num_teams];
// Initialize the list of teams.
int[] teams = new int[num_teams];
for (int i = 0; i < num_teams; i++) teams[i] = i;
// Start the rounds.
for (int round = 0; round < num_teams; round++)
{
for (int i = 0; i < n2; i++)
{
int team1 = teams[n2 - i];
int team2 = teams[n2 + i + 1];
results[team1, round] = team2;
results[team2, round] = team1;
}
// Set the team with the bye.
results[teams[0], round] = BYE;
// Rotate the array.
RotateArray(teams);
}
return results;
}
private int[,] GenerateRoundRobinEven(int num_teams)
{
// Generate the result for one fewer teams.
int[,] results = GenerateRoundRobinOdd(num_teams - 1);
// Copy the results into a bigger array,
// replacing the byes with the extra team.
int[,] results2 = new int[num_teams, num_teams - 1];
for (int team = 0; team < num_teams - 1; team++)
{
for (int round = 0; round < num_teams - 1; round++)
{
if (results[team, round] == BYE)
{
// Change the bye to the new team.
results2[team, round] = num_teams - 1;
results2[num_teams - 1, round] = team;
}
else
{
results2[team, round] = results[team, round];
}
}
}
return results2;
}
// Rotate the entries one position.
private void RotateArray(int[] teams)
{
int tmp = teams[teams.Length - 1];
Array.Copy(teams, 0, teams, 1, teams.Length - 1);
teams[0] = tmp;
}
private int[,] GenerateRoundRobin(int num_teams)
{
if (num_teams % 2 == 0)
return GenerateRoundRobinEven(num_teams);
else
return GenerateRoundRobinOdd(num_teams);
}
protected void Button1_Click(object sender, EventArgs e)
{
int[,] arr2D = GenerateRoundRobin(6);
System.Data.DataTable dt = ArraytoDatatable(arr2D);
GridView1.DataSource = dt;
GridView1.DataBind();
}
public static System.Data.DataTable ArraytoDatatable(int[,] numbers)
{
System.Data.DataTable dt = new System.Data.DataTable();
for (int i = 0; i < numbers.GetLength(1); i++)
{
dt.Columns.Add("Column" + (i + 1));
}
for (var i = 0; i < numbers.GetLength(0); ++i)
{
System.Data.DataRow row = dt.NewRow();
for (var j = 0; j < numbers.GetLength(1); ++j)
{
row[j] = numbers[i, j];
}
dt.Rows.Add(row);
}
return dt;
}
VB.Net
Private Const BYE As Integer = -1
Private Function GenerateRoundRobinOdd(ByVal num_teams As Integer) As Integer(,)
Dim n2 As Integer = CInt(((num_teams - 1) / 2))
Dim results As Integer(,) = New Integer(num_teams - 1, num_teams - 1) {}
Dim teams As Integer() = New Integer(num_teams - 1) {}
For i As Integer = 0 To num_teams - 1
teams(i) = i
Next
For round As Integer = 0 To num_teams - 1
For i As Integer = 0 To n2 - 1
Dim team1 As Integer = teams(n2 - i)
Dim team2 As Integer = teams(n2 + i + 1)
results(team1, round) = team2
results(team2, round) = team1
Next
results(teams(0), round) = BYE
RotateArray(teams)
Next
Return results
End Function
Private Function GenerateRoundRobinEven(ByVal num_teams As Integer) As Integer(,)
Dim results As Integer(,) = GenerateRoundRobinOdd(num_teams - 1)
Dim results2 As Integer(,) = New Integer(num_teams - 1, num_teams - 1 - 1) {}
For team As Integer = 0 To num_teams - 1 - 1
For round As Integer = 0 To num_teams - 1 - 1
If results(team, round) = BYE Then
results2(team, round) = num_teams - 1
results2(num_teams - 1, round) = team
Else
results2(team, round) = results(team, round)
End If
Next
Next
Return results2
End Function
Private Sub RotateArray(ByVal teams As Integer())
Dim tmp As Integer = teams(teams.Length - 1)
Array.Copy(teams, 0, teams, 1, teams.Length - 1)
teams(0) = tmp
End Sub
Private Function GenerateRoundRobin(ByVal num_teams As Integer) As Integer(,)
If num_teams Mod 2 = 0 Then
Return GenerateRoundRobinEven(num_teams)
Else
Return GenerateRoundRobinOdd(num_teams)
End If
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim arr2D As Integer(,) = GenerateRoundRobin(6)
Dim dt As Data.DataTable = ArraytoDatatable(arr2D)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Public Shared Function ArraytoDatatable(ByVal numbers As Integer(,)) As Data.DataTable
Dim dt As Data.DataTable = New Data.DataTable()
For i As Integer = 0 To numbers.GetLength(1) - 1
dt.Columns.Add("Column" & (i + 1))
Next
For i = 0 To numbers.GetLength(0) - 1
Dim row As Data.DataRow = dt.NewRow()
For j = 0 To numbers.GetLength(1) - 1
row(j) = numbers(i, j)
Next
dt.Rows.Add(row)
Next
Return dt
End Function
Screenshot