private void AddButton_Click(object sender, EventArgs e) { int rowsAdded = 0; for (int x = 0; x < GridViewAddToReports.RowCount; x++) { short position = 0; string selection = GridViewAddToReports.GetRowCellDisplayText(x, "Selected"); int ID = (int)GridViewAddToReports.GetRowCellValue(x, ColumnIDAddToReports); string strPos = (string)GridViewAddToReports.GetRowCellValue(x, GridColumnPosition); if ((selection == "Checked") && (!string.IsNullOrEmpty(strPos))) { object strPosition = GridViewAddToReports.GetRowCellValue(x, GridColumnPosition); //Get the selection from position combobox //If selection is First if (strPosition == (object)("First")) { if ((from mRptItem in context.MediaRptItem where mRptItem.REPORT_ID == ID orderby mRptItem.POSITION select mRptItem).Count() > 0) { //Bring the data from mediaRptItem with the selected reportID and increment the positions of remaining rows by 1 var items = context.MediaRptItem .Include(mri => mri.MEDIARPT) .Where(mri => mri.REPORT_ID == ID); foreach (MediaRptItem mRptItem in items) { mRptItem.MEDIARPT.ChgDate = DateTime.Now; mRptItem.POSITION = (Int16)(mRptItem.POSITION++); } } position = 1; //Newly added row is set at the top position } //If selection is last else if (strPosition == (object)("Last")) { int positionMax = Convert.ToInt32((from mRptItem in context.MediaRptItem where mRptItem.REPORT_ID == ID orderby mRptItem.POSITION select mRptItem.POSITION).Max()); //Set the position to the highest value position = (short)(positionMax + 1); } //If selection is <enter any number> else { try { short pos = short.Parse((GridViewAddToReports.GetRowCellValue(x, GridColumnPosition)).ToString()); int positionMax = Convert.ToInt32((from mRptItem in context.MediaRptItem where mRptItem.REPORT_ID == ID orderby mRptItem.POSITION select mRptItem.POSITION).Max()); //if entered number is larger than the max position in MediaRptItem set the position to the highest value if (pos > positionMax) { GridViewAddToReports.SetRowCellValue(x, GridColumnPosition, positionMax + 1); } else //Otherwise set the position value to the entered number { //Bring the data from mediaRptItem with rows having position equal to or greater than the entered position and increment their positions by 1 var items = context.MediaRptItem .Include(mri => mri.MEDIARPT) .Where(mri => mri.REPORT_ID == ID && mri.POSITION >= pos); foreach (MediaRptItem mRptItem in items) { mRptItem.MEDIARPT.ChgDate = DateTime.Now; mRptItem.POSITION = (Int16)(mRptItem.POSITION++); } GridViewAddToReports.SetRowCellValue(x, GridColumnPosition, pos); } position = pos; } catch (Exception ex) { MessageBox.Show("Please! Enter a position value to add the report"); } } if (position > 0) { MediaRptItem item = new MediaRptItem(); int reportId = (int)(GridViewAddToReports.GetRowCellValue(x, "ID")); item.REPORT_ID = reportId; item.SECTION_ID = mediaInfoSectID; item.POSITION = (short)position; context.AddToMediaRptItem(item); var rpt = context.MEDIARPT.FirstOrDefault(mr => mr.ID == reportId); if (rpt != null) { rpt.ChgDate = DateTime.Now; } context.SaveChanges(); mediaRptItemBindingSource.ResetBindings(false); GridViewAddToReports.RefreshData(); rowsAdded++; } } else if ((selection == "Checked") && (string.IsNullOrEmpty(strPos))) { MessageBox.Show("If the row is selected or a position is chosen a value must also be entered in the other column."); } } GridViewAddToReports.RefreshData(); if (rowsAdded > 0) { List <MEDIARPT> resultList = loadGrid(rcd); GridControlAddToReports.DataSource = resultList; } context.SaveChanges(); mediaRptItemBindingSource.ResetBindings(false); GridControlAddToReports.Refresh(); GridViewAddToReports.RefreshData(); panelControlStatus.Visible = true; if (rowsAdded > 1) { LabelStatus.Text = rowsAdded + " " + "reports added"; } else if (rowsAdded == 1) { LabelStatus.Text = rowsAdded + " " + "report added"; } else if (rowsAdded == 0) { LabelStatus.Text = "No reports added"; } rowStatusSave = new Timer(); rowStatusSave.Interval = 3000; rowStatusSave.Start(); rowStatusSave.Tick += TimedEventSave; }