ASP.NET Tutorial/ADO.net Database/DataView — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:56, 26 мая 2010

DataView Filter

<%@ Page language="c#" Inherits="DataViewFilter" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <body>
    <form method="post" runat="server" ID="Form1">
      <b><u>Filter = "ProductName = "Chocolade" "</u></b><br>
      <br>
      <asp:GridView runat="server" ID="Datagrid1" HeaderStyle-Font-Bold="true" />
      <br>
      <STRONG><U>Filter = "UnitsInStock = 0 AND UnitsOnOrder = 0" </U></STRONG>
      <br>
      <br>
      <asp:GridView runat="server" ID="Datagrid2" HeaderStyle-Font-Bold="true" />
      <br>
      <STRONG><U>Filter = <STRONG><U>"ProductName LIKE "P%""</U></STRONG></U></STRONG><br>
      <br>
      <asp:GridView runat="server" ID="Datagrid3" HeaderStyle-Font-Bold="true" />
    </form>
  </body>
</HTML>
File: Default.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DataViewFilter : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
    SqlConnection con = new SqlConnection(connectionString);
    string sql = "SELECT ProductID, ProductName, UnitsInStock, UnitsOnOrder, Discontinued FROM Products";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "Products");
    DataView view1 = new DataView(ds.Tables["Products"]);
    view1.RowFilter = "ProductName = "Chocolade"";
    Datagrid1.DataSource = view1;
    DataView view2 = new DataView(ds.Tables["Products"]);
    view2.RowFilter = "UnitsInStock = 0 AND UnitsOnOrder = 0";
    Datagrid2.DataSource = view2;
    DataView view3 = new DataView(ds.Tables["Products"]);
    view3.RowFilter = "ProductName LIKE "P%"";
    Datagrid3.DataSource = view3;
    this.DataBind();
  }
}


DataView Sort

<%@ Page language="c#" Inherits="DataViewSort" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <body>
    <form method="post" runat="server" ID="Form1">
      <b><u>Original order</u></b><br>
      <br>
      <asp:GridView runat="server" ID="Datagrid1" HeaderStyle-Font-Bold="true" />
      <br>
      <b><u>Sort = "LastName"</u></b><br>
      <br>
      <asp:GridView runat="server" ID="Datagrid2" HeaderStyle-Font-Bold="true" />
      <br>
      <b><u>Sort = "FirstName"</u></b><br>
      <br>
      <asp:GridView runat="server" ID="Datagrid3" HeaderStyle-Font-Bold="true" />
    </form>
  </body>
</HTML>
File: Default.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DataViewSort : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
    SqlConnection con = new SqlConnection(connectionString);
    string sql = "SELECT TOP 5 EmployeeID, TitleOfCourtesy, LastName, FirstName FROM Employees";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "Employees");
    Datagrid1.DataSource = ds.Tables["Employees"];
    DataView view2 = new DataView(ds.Tables["Employees"]);
    view2.Sort = "LastName";
    Datagrid2.DataSource = view2;
    DataView view3 = new DataView(ds.Tables["Employees"]);
    view3.Sort = "FirstName";
    Datagrid3.DataSource = view3;
    this.DataBind();
  }
}