Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView runat="server" ID="gvStudents"></asp:GridView><br />
<asp:Button Text="Set Role" runat="server" OnClick="OnClick" />
Code
C#
protected void OnClick(object sender, EventArgs e)
{
System.Data.DataTable dtStudent = new System.Data.DataTable();
dtStudent.Columns.AddRange(new System.Data.DataColumn[]
{
new System.Data.DataColumn("Name",typeof(string)),
new System.Data.DataColumn("Role",typeof(string)),
new System.Data.DataColumn("RoleValue",typeof(int))
});
dtStudent.Rows.Add("Student One");
dtStudent.Rows.Add("Student Two");
dtStudent.Rows.Add("Student Three");
dtStudent.Rows.Add("Student Four");
dtStudent.Rows.Add("Student Five");
dtStudent.Rows.Add("Student Six");
dtStudent.Rows.Add("Student Seven");
dtStudent.Rows.Add("Student Eight");
dtStudent.Rows.Add("Student Nine");
List<RoleData> list = GetRoles();
Random random = new Random();
for (int i = 0; i < dtStudent.Rows.Count; i++)
{
int index = random.Next(list.Count);
dtStudent.Rows[i]["Role"] = list[index].RoleName;
dtStudent.Rows[i]["RoleValue"] = list[index].RoleValue;
}
gvStudents.DataSource = dtStudent;
gvStudents.DataBind();
}
private static List<RoleData> GetRoles()
{
List<RoleData> roleNames = new List<RoleData>();
roleNames.Add(new RoleData { RoleName = "Role A", RoleValue = 52 });
roleNames.Add(new RoleData { RoleName = "Role B", RoleValue = 65 });
roleNames.Add(new RoleData { RoleName = "Role C", RoleValue = 29 });
return roleNames;
}
public class RoleData
{
public string RoleName { get; set; }
public int RoleValue { get; set; }
}
VB.Net
Protected Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim dtStudent As Data.DataTable = New Data.DataTable()
dtStudent.Columns.AddRange(New Data.DataColumn() {
New Data.DataColumn("Name", GetType(String)),
New Data.DataColumn("Role", GetType(String)),
New Data.DataColumn("RoleValue", GetType(Integer))})
dtStudent.Rows.Add("Student One")
dtStudent.Rows.Add("Student Two")
dtStudent.Rows.Add("Student Three")
dtStudent.Rows.Add("Student Four")
dtStudent.Rows.Add("Student Five")
dtStudent.Rows.Add("Student Six")
dtStudent.Rows.Add("Student Seven")
dtStudent.Rows.Add("Student Eight")
dtStudent.Rows.Add("Student Nine")
Dim list As List(Of RoleData) = GetRoles()
Dim random As Random = New Random()
For i As Integer = 0 To dtStudent.Rows.Count - 1
Dim index As Integer = random.[Next](list.Count)
dtStudent.Rows(i)("Role") = list(index).RoleName
dtStudent.Rows(i)("RoleValue") = list(index).RoleValue
Next
gvStudents.DataSource = dtStudent
gvStudents.DataBind()
End Sub
Private Shared Function GetRoles() As List(Of RoleData)
Dim roleNames As List(Of RoleData) = New List(Of RoleData)()
roleNames.Add(New RoleData With {.RoleName = "Role A", .RoleValue = 52})
roleNames.Add(New RoleData With {.RoleName = "Role B", .RoleValue = 65})
roleNames.Add(New RoleData With {.RoleName = "Role C", .RoleValue = 29})
Return roleNames
End Function
Public Class RoleData
Public Property RoleName As String
Public Property RoleValue As Integer
End Class
Screenshot