Hi nauna,
Refer below code.
HTML
<asp:GridView runat="server" ID="gvStudents"></asp:GridView>
<hr />
<asp:Button Text="Set Role" runat="server" OnClick="OnClick" />
Code
C#
protected void OnClick(object sender, EventArgs e)
{
DataTable dtStudent = new DataTable();
dtStudent.Columns.AddRange(new DataColumn[]
{
new DataColumn("Name"),
new DataColumn("Role")
});
dtStudent.Rows.Add(1, "Student One");
dtStudent.Rows.Add(2, "Student Two");
dtStudent.Rows.Add(3, "Student Three");
dtStudent.Rows.Add(4, "Student Four");
dtStudent.Rows.Add(5, "Student Five");
dtStudent.Rows.Add(6, "Student Six");
dtStudent.Rows.Add(7, "Student Seven");
dtStudent.Rows.Add(8, "Student Eight");
dtStudent.Rows.Add(9, "Student Nine");
dtStudent.Rows.Add(10, "Student Ten");
dtStudent.Rows.Add(11, "Student Eleven");
dtStudent.Rows.Add(12, "Student Twelve");
dtStudent.Rows.Add(13, "Student Thirteen");
dtStudent.Rows.Add(14, "Student Fourteen");
List<string> 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];
}
gvStudents.DataSource = dtStudent;
gvStudents.DataBind();
}
private static List<string> GetRoles()
{
List<string> roleNames = new List<string>();
roleNames.Add("Role A");
roleNames.Add("Role B");
roleNames.Add("Role C");
roleNames.Add("Role D");
roleNames.Add("Role E");
roleNames.Add("Role F");
roleNames.Add("Role G");
return roleNames;
}
VB.Net
Protected Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim dtStudent As DataTable = New DataTable()
dtStudent.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("Role")})
dtStudent.Rows.Add(1, "Student One")
dtStudent.Rows.Add(2, "Student Two")
dtStudent.Rows.Add(3, "Student Three")
dtStudent.Rows.Add(4, "Student Four")
dtStudent.Rows.Add(5, "Student Five")
dtStudent.Rows.Add(6, "Student Six")
dtStudent.Rows.Add(7, "Student Seven")
dtStudent.Rows.Add(8, "Student Eight")
dtStudent.Rows.Add(9, "Student Nine")
dtStudent.Rows.Add(10, "Student Ten")
dtStudent.Rows.Add(11, "Student Eleven")
dtStudent.Rows.Add(12, "Student Twelve")
dtStudent.Rows.Add(13, "Student Thirteen")
dtStudent.Rows.Add(14, "Student Fourteen")
Dim list As List(Of String) = 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)
Next
gvStudents.DataSource = dtStudent
gvStudents.DataBind()
End Sub
Private Shared Function GetRoles() As List(Of String)
Dim roleNames As List(Of String) = New List(Of String)()
roleNames.Add("Role A")
roleNames.Add("Role B")
roleNames.Add("Role C")
roleNames.Add("Role D")
roleNames.Add("Role E")
roleNames.Add("Role F")
roleNames.Add("Role G")
Return roleNames
End Function
Screenshot