... private void BuildButton_Click(object sender, EventArgs e) { if (data.matrixMode == RadioValues.All) { CreateMatrix(string.Empty, true); } else if (data.matrixMode == RadioValues.Speaker) { if (SpeakersListBox.SelectedIndex > -1) { CreateMatrix(SpeakersListBox.Text, true); } else { MessageBox.Show("You must select a speaker!", "No Speaker Selected", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } else // data.matrixMode == RadioValues.Unit { if (UnitsListBox.SelectedIndex > -1) { CreateMatrix(UnitsListBox.Text, false); } else { MessageBox.Show("You must select a unit!", "No Unit Selected", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } ... private void CreateMatrix(string speakerOrUnit, bool isSpeaker) { dataGrid.Columns.Clear(); dataGrid.Rows.Clear(); // Create a list of units with Bye spaces to equal number of speakers List unitsWithByes = new List(data.listUnits); for (int i = 0; i < (data.listSpeakers.Count - data.listUnits.Count); i++) { unitsWithByes.Add(""); } // Build matrix; the rows will be contain data in the following format {speaker, month1, month2, ...}; // the data in the month columns is the unit the speaker will be speaking at for that month string[][] matrix = new string[data.listSpeakers.Count][]; for (int i = 0; i < matrix.Length; i++) { matrix[i] = new string[data.intMonths + 1]; matrix[i][0] = data.listSpeakers[i]; for (int j = 0; j < data.intMonths; j++) { matrix[i][j + 1] = unitsWithByes[(j + i) % unitsWithByes.Count]; } } // Populate matrix columns string titleCol = "Speaker"; if (!isSpeaker) { titleCol = "Unit"; } dataGrid.Columns.Add(titleCol, titleCol); for (int i = 0; i < data.intMonths; i++) { dataGrid.Columns.Add("Month" + (i + 1), "Month" + (i + 1)); } // How many rows will be displayed in matrix int rows = data.listSpeakers.Count; if (!string.IsNullOrEmpty(speakerOrUnit)) { rows = 1; } // Populate matrix rows dataGrid.Rows.Add(rows); int count = 0; for (int i = 0; i < matrix.Length; i++) { if (string.IsNullOrEmpty(speakerOrUnit) || (isSpeaker && speakerOrUnit == matrix[i][0]) || (!isSpeaker && i == 0)) { DataGridViewRow row = dataGrid.Rows[count++]; row.Cells[titleCol].Value = (isSpeaker) ? matrix[i][0] : speakerOrUnit; for (int j = 1; j <= data.intMonths; j++) { string value = matrix[i][j]; if (!isSpeaker) { // Find speaker who has this unit in the jth month for (int k = 0; k < matrix.Length; k++) { if (matrix[k][j] == speakerOrUnit) { value = matrix[k][0]; } } } row.Cells["Month" + j].Value = value; } } } } ...