/// <summary> /// Next button validates data and moves to create pools /// </summary> private void btnNext_Click(object sender, EventArgs e) { // //Make sure all values are entered into datagrid view & //populate list of fencers // //clean & reset list listOfFencers = new List <Fencer>(); //Loop through each row for (int j = 0; j < dgvFencers.Rows.Count - 1; j++) { //Loop through each column for (int i = 0; i < dgvFencers.Rows[j].Cells.Count; i++) { //if one is null then report if (dgvFencers.Rows[j].Cells[i].Value == null || String.IsNullOrEmpty(dgvFencers.Rows[j].Cells[i].Value.ToString())) { //If not new row, report null data MessageBox.Show("Missing " + dgvFencers.Columns[i].HeaderText + " in ID #" + (dgvFencers.Rows[j].Index + 1)); return; } } //create new fencer Fencer tempFencer = new Fencer(); //populate values tempFencer.ID = Convert.ToInt32(dgvFencers.Rows[j].Cells["ID"].Value); tempFencer.FirstName = dgvFencers.Rows[j].Cells["FirstName"].Value.ToString(); tempFencer.LastName = dgvFencers.Rows[j].Cells["LastName"].Value.ToString(); //Add to list listOfFencers.Add(tempFencer); } //Go to next thing PoolSheet form = new PoolSheet(); form.Show(); }
private void btnCalculate_Click(object sender, EventArgs e) { //Grab Data System.Data.DataTable dataT = (System.Data.DataTable)(dgvPoolTable.DataSource); //get indexes int col1Index = dgvPoolTable.Columns["1"].Index; int colVIndex = dgvPoolTable.Columns["V"].Index; /////////////////////// // // Validate Input // /////////////////////// // // Make sure all the values are filled in // and are all integer values // //loop through columns for (int i = col1Index; i < colVIndex; i++) { //loop through rows for (int r = 0; r < dataT.Rows.Count; r++) { //get header number int hdrNumber = Convert.ToInt32(dataT.Columns[i].ColumnName.ToString()); int rowIDNumber = Convert.ToInt32(dataT.Rows[r]["#"].ToString()); //if row ID and i are equal, skip //i.e. black square if (hdrNumber == rowIDNumber) { continue; } //back sure it is numeric and not empty string val = dataT.Rows[r][i].ToString(); int n; //if nothing, stop if (val == null || string.IsNullOrEmpty(val)) { MessageBox.Show("Missing data"); return; } //if not an integer, stop else if (!int.TryParse(val, out n)) { MessageBox.Show("Invalid input"); return; } } } /////////////////////// // // Calculate results // /////////////////////// // //Touches Scored and Victories // //loop through rows for (int r = 0; r < dataT.Rows.Count; r++) { //reset & clear current values of Victories and Touches Scored int i_currentV = 0; int i_currentTS = 0; //loop through columns for (int i = col1Index; i < colVIndex; i++) { //get score string s_score = dataT.Rows[r][i].ToString(); //If victory, add to V column if (s_score == "V" || s_score == "5" || s_score == "V5") { //increase count by 1 i_currentV = 1 + i_currentV; //add to touches scored i_currentTS = 5 + i_currentTS; } //can't fence self else if (s_score == "") { continue; } //didn't win, under 5 points else { //get score int i_score = Convert.ToInt32(dataT.Rows[r][i]); //increase touches scored i_currentTS = i_score + i_currentTS; } } //set values in datatable dataT.Rows[r]["TS"] = i_currentTS; dataT.Rows[r]["V"] = i_currentV; // //Calculate touches Received // int i_currentTR = 0; string s_currentID = dataT.Rows[r]["#"].ToString(); int i_currentID = Convert.ToInt32(s_currentID); //Sum all the touches in the corresponding column for (int s_rw = 0; s_rw < dataT.Rows.Count; s_rw++) { //if not blank if (dataT.Rows[s_rw][s_currentID].ToString() != "") { //add to i_currentTR i_currentTR = i_currentTR + Convert.ToInt32(dataT.Rows[s_rw][s_currentID]); } } //set to final value dataT.Rows[r]["TR"] = i_currentTR; // //Calculate Indicator // int i_currentInd = i_currentTS - i_currentTR; dataT.Rows[r]["Ind"] = i_currentInd; //save values on Master List Fencer myFencer = listOfFencers.Find(x => x.ID == i_currentID); myFencer.pVictories = i_currentV; myFencer.pIndicator = i_currentInd; myFencer.pTouchesRec = i_currentTR; myFencer.pTouchesScored = i_currentTS; } //////////////////////////// // Calculate Current Places //////////////////////////// //Sort a list based on number of Victories List <Fencer> tempList = listOfFencers.OrderByDescending(o => o.pVictories) .ThenByDescending(p => p.pIndicator).ToList(); //loop through sorted list for (int j = 0; j < dataT.Rows.Count; j++) { //find the ID # int ID = Convert.ToInt32(dataT.Rows[j]["#"]); //find the matching place value //after sorting, if in place 0, then got 1st place int placeValue = tempList.IndexOf(tempList.Find(x => x.ID == ID)) + 1; //set value on DataTable dataT.Rows[j]["Pl"] = placeValue; } }