PageRenderTime 829ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2010/CSASPNETGridView/DataInMemory.aspx.cs

#
C# | 288 lines | 156 code | 50 blank | 82 comment | 19 complexity | cb6a142df40a3ffdecb5149ea8663a0f MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: DataInMemory.aspx.cs
  3. * Project: CSASPNETGridView
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The DataInMemory sample describes how to populate ASP.NET GridView
  7. * control with simple DataTable and how to implement Insert, Edit, Update,
  8. * Delete, Paging and Sorting functions in ASP.NET GridView control. The
  9. * DataTable is stored in ViewState to persist data across postbacks.
  10. *
  11. * This source is subject to the Microsoft Public License.
  12. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  13. * All other rights reserved.
  14. *
  15. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  16. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  18. \*****************************************************************************/
  19. #region Using directives
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Web;
  24. using System.Web.UI;
  25. using System.Web.UI.WebControls;
  26. using System.Data;
  27. #endregion Using directives
  28. namespace CSASPNETGridView
  29. {
  30. public partial class DataInMemory : System.Web.UI.Page
  31. {
  32. protected void Page_Load(object sender, EventArgs e)
  33. {
  34. // The Page is accessed for the first time.
  35. if (!IsPostBack)
  36. {
  37. // Initialize the DataTable and store it in ViewState.
  38. InitializeDataSource();
  39. // Enable the GridView paging option and specify the page size.
  40. gvPerson.AllowPaging = true;
  41. gvPerson.PageSize = 5;
  42. // Enable the GridView sorting option.
  43. gvPerson.AllowSorting = true;
  44. // Initialize the sorting expression.
  45. ViewState["SortExpression"] = "PersonID ASC";
  46. // Populate the GridView.
  47. BindGridView();
  48. }
  49. }
  50. // Initialize the DataTable.
  51. private void InitializeDataSource()
  52. {
  53. // Create a DataTable object named dtPerson.
  54. DataTable dtPerson = new DataTable();
  55. // Add four columns to the DataTable.
  56. dtPerson.Columns.Add("PersonID");
  57. dtPerson.Columns.Add("LastName");
  58. dtPerson.Columns.Add("FirstName");
  59. // Specify PersonID column as an auto increment column
  60. // and set the starting value and increment.
  61. dtPerson.Columns["PersonID"].AutoIncrement = true;
  62. dtPerson.Columns["PersonID"].AutoIncrementSeed = 1;
  63. dtPerson.Columns["PersonID"].AutoIncrementStep = 1;
  64. // Set PersonID column as the primary key.
  65. DataColumn[] dcKeys = new DataColumn[1];
  66. dcKeys[0] = dtPerson.Columns["PersonID"];
  67. dtPerson.PrimaryKey = dcKeys;
  68. // Add new rows into the DataTable.
  69. dtPerson.Rows.Add(null, "Davolio", "Nancy");
  70. dtPerson.Rows.Add(null, "Fuller", "Andrew");
  71. dtPerson.Rows.Add(null, "Leverling", "Janet");
  72. dtPerson.Rows.Add(null, "Dodsworth", "Anne");
  73. dtPerson.Rows.Add(null, "Buchanan", "Steven");
  74. dtPerson.Rows.Add(null, "Suyama", "Michael");
  75. dtPerson.Rows.Add(null, "Callahan", "Laura");
  76. // Store the DataTable in ViewState.
  77. ViewState["dtPerson"] = dtPerson;
  78. }
  79. private void BindGridView()
  80. {
  81. if (ViewState["dtPerson"] != null)
  82. {
  83. // Get the DataTable from ViewState.
  84. DataTable dtPerson = (DataTable)ViewState["dtPerson"];
  85. // Convert the DataTable to DataView.
  86. DataView dvPerson = new DataView(dtPerson);
  87. // Set the sort column and sort order.
  88. dvPerson.Sort = ViewState["SortExpression"].ToString();
  89. // Bind the GridView control.
  90. gvPerson.DataSource = dvPerson;
  91. gvPerson.DataBind();
  92. }
  93. }
  94. // GridView.RowDataBound Event
  95. protected void gvPerson_RowDataBound(object sender, GridViewRowEventArgs e)
  96. {
  97. // Make sure the current GridViewRow is a data row.
  98. if (e.Row.RowType == DataControlRowType.DataRow)
  99. {
  100. // Make sure the current GridViewRow is either
  101. // in the normal state or an alternate row.
  102. if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
  103. {
  104. // Add client-side confirmation when deleting.
  105. ((LinkButton)e.Row.Cells[1].Controls[0]).Attributes["onclick"] = "if(!confirm('Are you certain you want to delete this person ?')) return false;";
  106. }
  107. }
  108. }
  109. // GridView.PageIndexChanging Event
  110. protected void gvPerson_PageIndexChanging(object sender, GridViewPageEventArgs e)
  111. {
  112. // Set the index of the new display page.
  113. gvPerson.PageIndex = e.NewPageIndex;
  114. // Rebind the GridView control to
  115. // show data in the new page.
  116. BindGridView();
  117. }
  118. // GridView.RowEditing Event
  119. protected void gvPerson_RowEditing(object sender, GridViewEditEventArgs e)
  120. {
  121. // Make the GridView control into edit mode
  122. // for the selected row.
  123. gvPerson.EditIndex = e.NewEditIndex;
  124. // Rebind the GridView control to show data in edit mode.
  125. BindGridView();
  126. // Hide the Add button.
  127. lbtnAdd.Visible = false;
  128. }
  129. // GridView.RowCancelingEdit Event
  130. protected void gvPerson_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  131. {
  132. // Exit edit mode.
  133. gvPerson.EditIndex = -1;
  134. // Rebind the GridView control to show data in view mode.
  135. BindGridView();
  136. // Show the Add button.
  137. lbtnAdd.Visible = true;
  138. }
  139. // GridView.RowUpdating Event
  140. protected void gvPerson_RowUpdating(object sender, GridViewUpdateEventArgs e)
  141. {
  142. if (ViewState["dtPerson"] != null)
  143. {
  144. // Get the DataTable from ViewState.
  145. DataTable dtPerson = (DataTable)ViewState["dtPerson"];
  146. // Get the PersonID of the selected row.
  147. string strPersonID = gvPerson.Rows[e.RowIndex].Cells[2].Text;
  148. // Find the row in DateTable.
  149. DataRow drPerson = dtPerson.Rows.Find(strPersonID);
  150. // Retrieve edited values and updating respective items.
  151. drPerson["LastName"] = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox1")).Text;
  152. drPerson["FirstName"] = ((TextBox)gvPerson.Rows[e.RowIndex].FindControl("TextBox2")).Text;
  153. // Exit edit mode.
  154. gvPerson.EditIndex = -1;
  155. // Rebind the GridView control to show data after updating.
  156. BindGridView();
  157. // Show the Add button.
  158. lbtnAdd.Visible = true;
  159. }
  160. }
  161. // GridView.RowDeleting Event
  162. protected void gvPerson_RowDeleting(object sender, GridViewDeleteEventArgs e)
  163. {
  164. if (ViewState["dtPerson"] != null)
  165. {
  166. // Get the DataTable from ViewState.
  167. DataTable dtPerson = (DataTable)ViewState["dtPerson"];
  168. // Get the PersonID of the selected row.
  169. string strPersonID = gvPerson.Rows[e.RowIndex].Cells[2].Text;
  170. // Find the row in DateTable.
  171. DataRow drPerson = dtPerson.Rows.Find(strPersonID);
  172. // Remove the row from the DataTable.
  173. dtPerson.Rows.Remove(drPerson);
  174. // Rebind the GridView control to show data after deleting.
  175. BindGridView();
  176. }
  177. }
  178. // GridView.Sorting Event
  179. protected void gvPerson_Sorting(object sender, GridViewSortEventArgs e)
  180. {
  181. string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');
  182. // If the sorting column is the same as the previous one,
  183. // then change the sort order.
  184. if (strSortExpression[0] == e.SortExpression)
  185. {
  186. if (strSortExpression[1] == "ASC")
  187. {
  188. ViewState["SortExpression"] = e.SortExpression + " " + "DESC";
  189. }
  190. else
  191. {
  192. ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
  193. }
  194. }
  195. // If sorting column is another column,
  196. // then specify the sort order to "Ascending".
  197. else
  198. {
  199. ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
  200. }
  201. // Rebind the GridView control to show sorted data.
  202. BindGridView();
  203. }
  204. protected void lbtnAdd_Click(object sender, EventArgs e)
  205. {
  206. // Hide the Add button and showing Add panel.
  207. lbtnAdd.Visible = false;
  208. pnlAdd.Visible = true;
  209. }
  210. protected void lbtnSubmit_Click(object sender, EventArgs e)
  211. {
  212. if (ViewState["dtPerson"] != null)
  213. {
  214. // Get the DataTable from ViewState and inserting new data to it.
  215. DataTable dtPerson = (DataTable)ViewState["dtPerson"];
  216. // Add the new row.
  217. dtPerson.Rows.Add(null, tbLastName.Text, tbFirstName.Text);
  218. // Rebind the GridView control to show inserted data.
  219. BindGridView();
  220. }
  221. // Empty the TextBox controls.
  222. tbLastName.Text = "";
  223. tbFirstName.Text = "";
  224. // Show the Add button and hiding the Add panel.
  225. lbtnAdd.Visible = true;
  226. pnlAdd.Visible = false;
  227. }
  228. protected void lbtnCancel_Click(object sender, EventArgs e)
  229. {
  230. // Empty the TextBox controls.
  231. tbLastName.Text = "";
  232. tbFirstName.Text = "";
  233. // Show the Add button and hiding the Add panel.
  234. lbtnAdd.Visible = true;
  235. pnlAdd.Visible = false;
  236. }
  237. }
  238. }