Bu kodda, bir DataTable’dan bir DataView oluşturabilirsiniz, ancak olağandışı bir bükülme ile:
DataTable’daki hangi satırların DataView’e dahil edileceğine satır bazında (kodla) karar verirsiniz.
.Net framework’s DataView represents a databindable, customized view of a DataTable for filtering, sorting, searching, editing and navigation. DataView does not store any data, it represents a connected view of its corresponding DataTable. DataTable represents one table of in-memory data.
Example Output:
ASP.NET
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | <%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html> <script runat="server"> protected void Button1_Click(object sender, System.EventArgs e) { DataTable dt = new DataTable(); dt.TableName = "Books"; DataColumn dc1 = new DataColumn(); dc1.ColumnName = "BookID"; dc1.DataType = typeof(int); dc1.AllowDBNull = false; dc1.Unique = true; DataColumn dc2 = new DataColumn(); dc2.ColumnName = "Category"; dc2.DataType = typeof(string); DataColumn dc3 = new DataColumn(); dc3.ColumnName = "Book Name"; dc3.DataType = typeof(string); DataColumn dc4 = new DataColumn(); dc4.ColumnName = "Author"; dc4.DataType = typeof(string); dt.Columns.AddRange(new DataColumn[] { dc1, dc2, dc3, dc4 }); dt.Rows.Add(new object[] { 1, "iPhone", "iPhone User Interface Cookbook: RAW", "Cameron Banga" }); dt.Rows.Add(new object[] { 2, "MySQL", "MySQL 5.1 Plugin Development", "Andrew Hutchings, Sergei Golubchik" }); dt.Rows.Add(new object[] { 3, "MySQL", "MySQL Admin Cookbook", "Daniel Schneller, Udo Schwedt" }); dt.AcceptChanges(); Label1.Text = "Source DataTable"; GridView1.DataSource = dt.DefaultView; GridView1.DataBind(); DataView dView = new DataView(dt); dView.RowFilter = "Category = 'MySQL'"; Label2.Text = "Here we create a new DataView<br /> and set the RowFilter (Category = 'MySQL'"; GridView2.DataSource = dView; GridView2.DataBind(); DataTable dt2 = dView.ToTable(); Label3.Text = "Here we create a new DataTable from DataView"; GridView3.DataSource = dt2; GridView3.DataBind(); } </script> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> CODE4EXAMPLE.COM </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> How to use DataView ToTable method to create a new DataTable in Ado.NET </h2> <hr color="CornFlowerBlue" /> <asp:Label ID="Label1" runat="server" Font-Size="Large" CssClass="alert alert-primary mb-1 d-block" > </asp:Label> <asp:GridView ID="GridView1" runat="server" CssClass="table table-striped" > </asp:GridView> <br /> <asp:Label ID="Label2" runat="server" CssClass="alert alert-warning mb-1 d-block" ></asp:Label> <asp:GridView ID="GridView2" runat="server" CssClass="table table-striped table-dark" > </asp:GridView> <br /> <asp:Label ID="Label3" runat="server" CssClass="alert alert-warning mb-1 d-block" > </asp:Label> <asp:GridView ID="GridView3" runat="server" CssClass="table table-striped table-dark" > </asp:GridView> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Fill GridView" CssClass="btn btn-primary btn-lg" /> </div> </form> </div> </body> </html> |