DataTable Select() method.
Output:
ASP.NET Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <%@ Page Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html> <script runat="server"> void Button1_Click(object sender, System.EventArgs e) { DataTable dt = new DataTable(); dt.TableName = "Products"; DataColumn dc1 = new DataColumn(); dc1.ColumnName = "ProductID"; dc1.DataType = typeof(int); dc1.AllowDBNull = false; dc1.Unique = true; DataColumn dc2 = new DataColumn(); dc2.ColumnName = "ProductName"; dc2.DataType = typeof(string); DataColumn dc3 = new DataColumn(); dc3.ColumnName = "Price"; dc3.DataType = typeof(decimal); dt.Columns.AddRange(new DataColumn[] { dc1,dc2,dc3 }); dt.Rows.Add(new object[] { 1, "NetBook", "500" }); dt.Rows.Add(new object[] { 2, "Monitor", "100" }); dt.Rows.Add(new object[] { 3, "Laptop", "700.25" }); GridView1.DataSource = dt; GridView1.DataBind(); DataRow[] drows = dt.Select(); for (int i = 0; i < drows.Length;i++ ) { decimal productPrice = Convert.ToDecimal(dt.Rows[i]["Price"].ToString()); dt.Rows[i]["Price"] = productPrice+(productPrice/100*10); } Label1.Text = "After updating rows (increasing price 10%)"; GridView2.DataSource = dt; GridView2.DataBind(); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>How to use DataTable.Select method in ado.net</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <form id="form1" runat="server"> <div> <h2 style="color:DarkBlue; font-style:italic;"> How to use DataTable Select() method in ado.net </h2> <hr align="left" color="CornFlowerBlue" /> <asp:GridView ID="GridView1" runat="server" CssClass="table table-striped" > </asp:GridView> <br /> <asp:Label ID="Label1" runat="server" CssClass="alert alert-primary mb-1 d-block" > </asp:Label> <br /><br /> <asp:GridView ID="GridView2" runat="server" CssClass="table table-striped" > </asp:GridView> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Populate GridView" CssClass="btn btn-primary btn-lg" /> </div> </form> </div> </body> </html> |