2015年6月9日 星期二

how to edit,update row in gridview

請參考來源:how to edit,update row in gridview
---

  1. <%@ Page Language="C#" AutoEventWireup="true"
  2. CodeFile="GridViewComplete.aspx.cs"
  3. Inherits="GridViewComplete" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head id="Head1" runat="server">
  8. <title>Grid View Add Update Delete</title>
  9. </head>
  10. <body>
  11. <form id="form1" runat="server">
  12. <div>
  13. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
  14. ShowFooter="true" AllowPaging="true" PageSize="4"
  15. AllowSorting="True"
  16. OnRowCommand="GridView1_RowCommand"
  17. OnPageIndexChanging="GridView1_PageIndexChanging"
  18. OnRowDeleting="GridView1_RowDeleting"
  19. OnRowEditing="GridView1_RowEditing"
  20. OnRowUpdating="GridView1_RowUpdating"
  21. OnSorting="GridView1_Sorting"
  22. OnRowCancelingEdit="GridView1_RowCancelingEdit">
  23. <Columns>
  24. <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
  25. <asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
  26. <EditItemTemplate>
  27. <asp:Label ID="Label1" runat="server"
  28. Text='<%# Eval("Id") %>'></asp:Label>
  29. </EditItemTemplate>
  30. <ItemTemplate>
  31. <asp:Label ID="Label1" runat="server"
  32. Text='<%# Bind("Id") %>'></asp:Label>
  33. </ItemTemplate>
  34. </asp:TemplateField>
  35. <asp:BoundField DataField="Id" ShowHeader="True"/>
  36. <asp:TemplateField HeaderText="Name" SortExpression="Name">
  37. <EditItemTemplate>
  38. <asp:TextBox ID="TextBox1" runat="server"
  39. Text='<%# Bind("Name") %>'></asp:TextBox>
  40. </EditItemTemplate>
  41. <ItemTemplate>
  42. <asp:Label ID="Label2" runat="server"
  43. Text='<%# Bind("Name") %>'></asp:Label>
  44. </ItemTemplate>
  45. <FooterTemplate>
  46. <asp:TextBox ID="QuantityTextBox" runat="server"></asp:TextBox>
  47. </FooterTemplate>
  48. </asp:TemplateField>
  49. <asp:TemplateField HeaderText="Description" SortExpression="Description">
  50. <EditItemTemplate>
  51. <asp:TextBox ID="TextBox2" runat="server"
  52. Text='<%# Bind("Description") %>'></asp:TextBox>
  53. </EditItemTemplate>
  54. <ItemTemplate>
  55. <asp:Label ID="Label3" runat="server"
  56. Text='<%# Bind("Description") %>'></asp:Label>
  57. </ItemTemplate>
  58. <FooterTemplate>
  59. <asp:TextBox ID="DescriptionTextBox" runat="server"></asp:TextBox>
  60. </FooterTemplate>
  61. </asp:TemplateField>
  62. <asp:TemplateField>
  63. <FooterTemplate>
  64. <asp:LinkButton ID="btnNew" runat="server"
  65. CommandName="New" Text="New" />
  66. </FooterTemplate>
  67. </asp:TemplateField>
  68. </Columns>
  69. </asp:GridView>
  70. </div>
  71. <div style="color:Red">
  72. <asp:Label ID="lblMsg" runat="server"></asp:Label>
  73. </div>
  74. </form>
  75. </body>
  76. </html>
程式區:
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class GridViewComplete : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. if (!IsPostBack)
  16. {
  17. BindGrid();
  18. }
  19. }
  20. public void BindGrid()
  21. {
  22. if (Session["dt"] == null)
  23. {
  24. GridView1.DataSource = CreateDGDataSource();
  25. GridView1.DataBind();
  26. }
  27. else
  28. {
  29. GridView1.DataSource = Session["dt"] as DataTable;
  30. GridView1.DataBind();
  31. }
  32. }
  33. public DataTable CreateDGDataSource()
  34. {
  35. // Create sample data for the DataList control.
  36. DataTable dt = new DataTable();
  37. DataRow dr;
  38. int i;
  39. int y;
  40. // Define the columns of the table.
  41. dt.Columns.Add(new DataColumn("ID", typeof(int)));
  42. dt.Columns.Add(new DataColumn("Name", typeof(string)));
  43. dt.Columns.Add(new DataColumn("Description", typeof(string)));
  44. //Make some rows and put some sample data in
  45. for (i = 1; i <= 5; i++)
  46. {
  47. dr = dt.NewRow();
  48. dr[0] = i;
  49. dr[1] = "Name" + "-" + i;
  50. dr[2] = "Item " + "_" + i;
  51. //add the row to the datatable
  52. dt.Rows.Add(dr);
  53. }
  54. Session["y"] = i;
  55. Session["dt"] = dt;
  56. return dt;
  57. }
  58. public ICollection CreateDGDataSource(int CategoryID)
  59. {
  60. DataView dv = new DataView(CreateDGDataSource(), "ID=" + CategoryID, null,
  61. DataViewRowState.CurrentRows);
  62. return dv;
  63. }
  64. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  65. {
  66. try
  67. {
  68. if (e.CommandName.Equals("New"))
  69. {
  70. LinkButton btnNew = e.CommandSource as LinkButton;
  71. GridViewRow row = btnNew.NamingContainer as GridViewRow;
  72. if (row == null)
  73. {
  74. return;
  75. }
  76. TextBox txtCatName = row.FindControl("QuantityTextBox") as TextBox;
  77. TextBox txtDescription = row.FindControl("DescriptionTextBox") as TextBox;
  78. DataTable dt = Session["dt"] as DataTable;
  79. DataRow dr;
  80. int intId = (int)Session["y"];
  81. dr = dt.NewRow();
  82. dr["Id"] = intId++;
  83. Session["y"] = intId;
  84. dr["Name"] = txtCatName.Text;
  85. dr["Description"] = txtDescription.Text;
  86. dt.Rows.Add(dr);
  87. dt.AcceptChanges();
  88. Session["dt"] = dt;
  89. GridView1.DataSource = Session["dt"] as DataTable;
  90. GridView1.DataBind();
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. }
  96. }
  97. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  98. {
  99. GridView1.PageIndex = e.NewPageIndex;
  100. BindGrid();
  101. }
  102. protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
  103. {
  104. DataTable dataTable = Session["dt"] as DataTable;
  105. if (dataTable != null)
  106. {
  107. DataView dataView = new DataView(dataTable);
  108. dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
  109. GridView1.DataSource = dataView;
  110. GridView1.DataBind();
  111. }
  112. }
  113. private string ConvertSortDirectionToSql(SortDirection sortDireciton)
  114. {
  115. string newSortDirection = String.Empty;
  116. switch (sortDireciton)
  117. {
  118. case SortDirection.Ascending:
  119. newSortDirection = "ASC";
  120. break;
  121. case SortDirection.Descending:
  122. newSortDirection = "DESC";
  123. break;
  124. }
  125. return newSortDirection;
  126. }
  127. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  128. {
  129. int ID = (int)GridView1.DataKeys[e.RowIndex].Value;
  130. // Query the database and get the values based on the ID and delete it.
  131. lblMsg.Text = "Deleted Record Id" +ID.ToString();
  132. }
  133. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  134. {
  135. GridView1.EditIndex = e.NewEditIndex;
  136. BindGrid();
  137. }
  138. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  139. {
  140. // Retrieve the row being edited.
  141. int index = GridView1.EditIndex;
  142. GridViewRow row = GridView1.Rows[index];
  143. TextBox t1 = row.FindControl("TextBox1") as TextBox;
  144. TextBox t2 = row.FindControl("TextBox2") as TextBox;
  145. string t3 = GridView1.DataKeys[e.RowIndex].Value.ToString();
  146. lblMsg.Text = "Updated record " + t1.Text + "," + t2.Text + "Value From Bound Field" + t3;
  147. }
  148. protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  149. {
  150. GridView1.EditIndex = -1;
  151. BindGrid();
  152. }
  153. }

沒有留言:

張貼留言