private void button1_Click_1(object sender, EventArgs e) { numberOfGroups = Int32.Parse(textBox1.Text); textBox1.Text = ""; var adapter = new StudentTableAdapter(); var rows = adapter.GetData(); List <Students> students = new List <Students>(); foreach (var row in rows) { Students student = new Students(); student.firstname = row.First_Name; student.lastname = row.Last_Name; students.Add(student); } List <List <Students> > groups = new List <List <Students> >(); for (int g = 0; g < numberOfGroups; g++) { groups.Add(new List <Students>()); } Random random; while (students.Count > 0) { for (int n = 0; n < numberOfGroups; n++) { random = new Random(); int rand = random.Next(0, students.Count); //Console.WriteLine(students[n].firstname); groups[n].Add(students[rand]); students.RemoveAt(rand); } } int i = 1; foreach (var group in groups) { listBox1.Items.Add("Group #" + i); foreach (var person in group) { listBox1.Items.Add(person.firstname + " " + person.lastname); } listBox1.Items.Add("\n"); i++; } }
private void subButton_Click(object sender, EventArgs e) { if (checkBox.GetItemChecked(0)) { MessageBox.Show("Generating groups based on the number of students!"); checkBox.SetSelected(0, false); } else if (checkBox.GetItemChecked(1)) { MessageBox.Show("Generating groups based on the number of groups!"); checkBox.SetSelected(1, false); int numberOfGroups = Int32.Parse(numLbl.Text); numLbl.Text = ""; var adapter = new StudentTableAdapter(); var rows = adapter.GetData(); List <Students> students = new List <Students>(); foreach (var row in rows) { int n = 0; List <Students> student = new List <Students>(); student.Add(new Students() { firstname = row.First_Name, lastname = row.Last_Name }); //student[n].firstname = row.First_Name; //student[n].lastname = row.Last_Name; students.Add(student[n]); n++; } List <List <Students> > groups = new List <List <Students> >(); for (int g = 0; g < numberOfGroups; g++) { groups.Add(new List <Students>()); } Random random; while (students.Count > 0) { for (int n = 0; n < numberOfGroups; n++) { random = new Random(); int rand = random.Next(0, students.Count); //Console.WriteLine(students[n].firstname); groups.ElementAt(n).Add(students[rand]); students.RemoveAt(rand); } } int i = 1; foreach (var group in groups) { listBox1.Items.Add("Group #" + i); foreach (var person in group) { listBox1.Items.Add(person.firstname + " " + person.lastname); } listBox1.Items.Add("\n"); i++; } } }