Csharp/CSharp Tutorial/ADO.Net/DataView
Содержание
Attach DataViews to two separate DataGrid controls
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class MainClass : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.DataGrid dataGrid2; public MainClass() { InitializeComponent(); string ConnectionString ="Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=localhost;"; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); DataSet ds = new DataSet("Orders"); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Orders", conn); adapter.Fill(ds, "Orders"); DataView dataView1 = new DataView(ds.Tables["Orders"]); dataView1.Sort = "ShippedDate ASC"; DataView dataView2 = new DataView(ds.Tables["Orders"]); dataView2.Sort = "EmployeeID ASC"; dataGrid1.DataSource = dataView1; dataGrid2.DataSource = dataView2; } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.dataGrid2 = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 8); this.dataGrid1.Size = new System.Drawing.Size(272, 248); // // dataGrid2 // this.dataGrid2.DataMember = ""; this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid2.Location = new System.Drawing.Point(288, 8); this.dataGrid2.Size = new System.Drawing.Size(312, 256); // // MainClass // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(600, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid2, this.dataGrid1}); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid2)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new MainClass()); }
}</source>
Create DataViews from DataTable
<source lang="csharp">using System; using System.Data; using System.Data.SqlClient; class MainClass {
static void Main(string[] args) { string connString = @"server = .\sqlexpress;integrated security = true;database = northwind"; string sql = @"select contactname, country from customers"; SqlConnection conn = new SqlConnection(connString); try { SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); DataTable dt = ds.Tables["customers"]; DataView dv = new DataView(dt,"country = "Germany"","country",DataViewRowState.CurrentRows); foreach (DataRowView drv in dv) { for (int i = 0; i < dv.Table.Columns.Count; i++) Console.WriteLine(drv[i]); } } catch(Exception e) { Console.WriteLine("Error: " + e); } finally { conn.Close(); } }
}</source>
DataView Sorter and Filter
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; public class DataViewManagerDemo : System.Windows.Forms.Form {
private System.Windows.Forms.DataGrid dataGrid1; public DataViewManagerDemo() { InitializeComponent(); } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 8); this.dataGrid1.Size = new System.Drawing.Size(448, 328); // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(480, 341); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1}); this.Load += new System.EventHandler(this.DataViewManagerDemo_Load); ((System.ruponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DataViewManagerDemo()); } private void DataViewManagerDemo_Load(object sender, System.EventArgs e) { string ConnectionString ="Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=localhost;"; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); DataSet ds = new DataSet("Orders"); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Orders", conn); adapter.Fill(ds, "Orders"); DataViewManager dvm = new DataViewManager(ds); dvm.DataViewSettings["Orders"].RowFilter = "EmployeeID = 4"; dvm.DataViewSettings["Orders"].Sort = "ShippedDate ASC"; DataView dataView1 = dvm.CreateDataView(ds.Tables["Orders"]); dataGrid1.DataSource = dataView1; }
}</source>
Loop through DataRowView in DataView
<source lang="csharp">using System; using System.Data; using System.Data.SqlClient; class MainClass {
static void Main(string[] args) { string connString = @"server = .\sqlexpress;integrated security = true;database = northwind"; string sql = @"select contactname, country from customers"; SqlConnection conn = new SqlConnection(connString); try { SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); DataTable dt = ds.Tables["customers"]; DataView dv = new DataView(dt,"country = "Germany"","country",DataViewRowState.CurrentRows); foreach (DataRowView drv in dv) { for (int i = 0; i < dv.Table.Columns.Count; i++) Console.WriteLine(drv[i]); } } catch(Exception e) { Console.WriteLine("Error: " + e); } finally { conn.Close(); } }
}</source>
Multiple Data View
<source lang="csharp">using System.Data.SqlClient; using System.Data; using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms;
public class MultipleView : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid gridArgentina; private System.Windows.Forms.DataGrid gridBrazil; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.DataGrid gridAll; public MultipleView() { this.gridArgentina = new System.Windows.Forms.DataGrid(); this.gridBrazil = new System.Windows.Forms.DataGrid(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.gridAll = new System.Windows.Forms.DataGrid(); ((System.ruponentModel.ISupportInitialize)(this.gridArgentina)).BeginInit(); ((System.ruponentModel.ISupportInitialize)(this.gridBrazil)).BeginInit(); ((System.ruponentModel.ISupportInitialize)(this.gridAll)).BeginInit(); this.SuspendLayout(); // // gridArgentina // this.gridArgentina.DataMember = ""; this.gridArgentina.Location = new System.Drawing.Point(12, 32); this.gridArgentina.Size = new System.Drawing.Size(416, 88); this.gridArgentina.TabIndex = 0; // // gridBrazil // this.gridBrazil.DataMember = ""; this.gridBrazil.Location = new System.Drawing.Point(12, 156); this.gridBrazil.Size = new System.Drawing.Size(416, 96); this.gridBrazil.TabIndex = 1; // // label1 // this.label1.Location = new System.Drawing.Point(12, 12); this.label1.Size = new System.Drawing.Size(156, 16); this.label1.Text = "Customers From Argentina:"; // // label2 // this.label2.Location = new System.Drawing.Point(12, 136); this.label2.Size = new System.Drawing.Size(156, 16); this.label2.Text = "Customers From Brazil:"; // // label3 // this.label3.Location = new System.Drawing.Point(12, 268); this.label3.Size = new System.Drawing.Size(156, 16); this.label3.Text = "All Customers:"; // // gridAll // this.gridAll.DataMember = ""; this.gridAll.Location = new System.Drawing.Point(12, 288); this.gridAll.Size = new System.Drawing.Size(416, 96); // // MultipleView // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(440, 398); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.gridAll, this.label3, this.label2, this.label1, this.gridBrazil, this.gridArgentina}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Text = "Multiple Views"; this.Load += new System.EventHandler(this.MultipleView_Load); ((System.ruponentModel.ISupportInitialize)(this.gridArgentina)).EndInit(); ((System.ruponentModel.ISupportInitialize)(this.gridBrazil)).EndInit(); ((System.ruponentModel.ISupportInitialize)(this.gridAll)).EndInit(); this.ResumeLayout(false); } private void MultipleView_Load(object sender, System.EventArgs e) { string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI"; string SQL = "SELECT * FROM Customers"; SqlConnection con = new SqlConnection(connectionString); SqlCommand com = new SqlCommand(SQL, con); SqlDataAdapter adapter = new SqlDataAdapter(com); DataSet ds = new DataSet("Northwind"); con.Open(); adapter.Fill(ds, "Customers"); con.Close(); DataView viewArgentina = new DataView(ds.Tables["Customers"]); DataView viewBrazil = new DataView(ds.Tables["Customers"]); viewArgentina.RowFilter = "Country = "Argentina""; viewBrazil.RowFilter = "Country = "Brazil""; gridArgentina.DataSource = viewArgentina; gridBrazil.DataSource = viewBrazil; gridAll.DataSource = ds.Tables["Customers"].DefaultView; } [STAThread] static void Main() { Application.Run(new MultipleView()); } }</source>