// //VIEW INCIDENT DETAILS // #region VIEW INCIDENT DETAILS // //SET FORMVIEW_INCIDENTS /// <summary> /// Set the form view to display the selected incident out of all the other incidents for the selected customer /// </summary> protected void setFormView_Incidents() { //Get all the records loaded in the formview DataRowView view = (DataRowView)this.FormView_Incidents.DataItem; if (view != null) { //Get the selected value of the record which is the incident id object id = this.GridView_Incidents.SelectedValue; //For loop that iterates through the row's cells for (int i = 0; i < view.DataView.Count; i++) { //If the content of the first cell, which is the incident id, is equal to the id got from the //selected row in the gridview: if (view.DataView[i].Row[0].Equals(id)) { //The amount of rows/records loaded in the formview and the rows in the dataview object are the //same so i can use the counter of the loop to set the index for the displayed page FormView_Incidents.PageIndex = i; //The DataBind is necessary otherwise the formview won't display the desired record FormView_Incidents.DataBind(); } } } }
// //BTN CRAETE NEW INCIDENT CLICK /// <summary> /// Insert the new record in the database and update the gridview and formview accordingly /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnCreateNewIncident_Click(object sender, EventArgs e) { try { //Call the Insert method against the SqlDataSource_Incidents which then updates the database SqlDataSource_Incidents.Insert(); //Refresh both the gridview and formview with the new incident GridView_Incidents.DataBind(); FormView_Incidents.DataBind(); //Bring back to the incident list page this.Incidents_MultiView.ActiveViewIndex = 0; //Reset the form resetNewIncidentForm(); } catch (InvalidOperationException ex) { Session["Exception"] = ex; } catch (Exception ex) { Session["Exception"] = ex; } }
// //UPDATE INCIDENT // #region UPDATE INCIDENT // //BTN UPDATE CLICK /// <summary> /// Perform the updating of the selected incident in the database and then refresh the gridview and the formview /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnUpdate_Click(object sender, EventArgs e) { try { //Call the Update method of SqlDataSource_FormViewIncident which executes the update query updating the //selected record SqlDataSource_FormViewIncident.Update(); //Call the DataBind method for both the gridview and the formview refreshing the incident with the new changes GridView_Incidents.DataBind(); FormView_Incidents.DataBind(); //Display the view incident details page this.Incidents_MultiView.ActiveViewIndex = 1; //Reset the form ResetUpdateForm(); } catch (InvalidOperationException ex) { Session["Exception"] = ex; } catch (Exception ex) { Session["Exception"] = ex; } }
// //SET UPDATE FORM /// <summary> /// Set the update form by loading all the selected incident's detail into it /// </summary> protected void setUpdateForm() { //String vars to store the column name, the cell value and the textbox text string colName = string.Empty, cellValue = string.Empty, text = string.Empty; bool ok = false; //NB: it is necessary otherwise the DataItem will be null FormView_Incidents.DataBind(); //Get all the records stored in the formview DataRowView row = (DataRowView)this.FormView_Incidents.DataItem; //In case the datarowview contains the data if (row != null) { //Get the displayed page index (the one where the record to be updated is displayed) int index = (int)FormView_Incidents.PageIndex; //Get the count of how many cell are contained in one row int cellsCount = this.GridView_Incidents.SelectedRow.Cells.Count; //Declare a dictionary where the record's attributes will be paired with the gridview columns header as //keys Dictionary <string, string> selectedRow = new Dictionary <string, string>(); //Populates the dictionary for (int i = 1; i < cellsCount - 1; i++) { //Get the gridview's columns headers colName = this.GridView_Incidents.Columns[i].HeaderText; //Get the content of each cell cellValue = row.DataView[index].Row[i].ToString(); //Little fix: the date uploaded in the formview once they are retrieved returns to the datetime format // and to fix this i call the DateFormat custom method which format all the date that are // successfully parsed into the dd/MM/yyyy format. In all the other cases the values are returned // as they are. cellValue = DateFormat(cellValue); //Add the couples to the dictionary selectedRow.Add(colName, cellValue); } //In this loop i take advantage of the fact that i placed both in the selectedRow dictionary, which //holds the record's details, and the ctrlList dictionary the gridview column's headers as keys: //in this way i can get precisely the right match for both the control and its associated value foreach (KeyValuePair <string, string> pair in selectedRow) { //Get the value of each cell from the selectedRos dictionary and stores it in the text var ok = selectedRow.TryGetValue(pair.Key, out text); //If the value is successfully retrieved if (ok) { //Initialize a flag textbox control TextBox ctrl; //Set the flag textbox with each textbox contained in the textboxes dictionary ctrlList.TryGetValue(pair.Key, out ctrl); //If the flag textboxes has been effectively set with a textbox if (ctrl is TextBox) { //Set its text property with the value retrieved from the selectedRow dictionary if (text != " ") { (ctrl as TextBox).Text = text; } } } } //Refresh both the products and the techs dropdownlist so that they will diplay all the product registered //to the selected customer and all the available tech level 2 ordered in descendant order according the //amount of incidents assigned to each one. //NB: in the tech dropdownlist the selected item is the tech assigned to the record to be updated DdlProds.DataBind(); DdlTech.DataBind(); //Dispose the dictionary selectedRow = null; } }