Csharp/C Sharp by API/System.Data/DataViewManager
DataViewManager.CreateDataView
<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>
DataViewManager.DataViewSettings
<source lang="csharp"> using System; using System.Configuration; using System.Data; using System.Data.rumon; using System.Data.SqlClient; using System.Windows.Forms; class Form1 : Form {
public Form1() { InitializeComponent(); } private void getData_Click(object sender, EventArgs e) { string orders = "SELECT * FROM Orders"; string customers = "SELECT * FROM Customers"; using (SqlConnection con = new SqlConnection("northwind ConnectionString")) { SqlDataAdapter da = new SqlDataAdapter(orders, con); DataSet ds = new DataSet(); da.Fill(ds, "Orders"); da = new SqlDataAdapter(customers, con); da.Fill(ds, "Customers"); ds.Relations.Add("CustomerOrders", ds.Tables["Customers"].Columns["CustomerID"], ds.Tables["Orders"].Columns["CustomerID"]); DataViewManager dvm = new DataViewManager(ds); dvm.DataViewSettings["Customers"].RowFilter = "Country="UK""; dataGrid.SetDataBinding(dvm, "Customers"); } } private void InitializeComponent() { this.getData = new System.Windows.Forms.Button(); this.dataGrid = new System.Windows.Forms.DataGrid(); this.SuspendLayout(); // // getData // this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.getData.Location = new System.Drawing.Point(684, 558); this.getData.Name = "getData"; this.getData.TabIndex = 0; this.getData.Text = "Get Data"; this.getData.Click += new System.EventHandler(this.getData_Click); // // dataGrid // this.dataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.dataGrid.Location = new System.Drawing.Point(13, 13); this.dataGrid.Name = "dataGrid"; this.dataGrid.Size = new System.Drawing.Size(745, 534); this.dataGrid.TabIndex = 1; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(771, 593); this.Controls.Add(this.dataGrid); this.Controls.Add(this.getData); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); }
private System.Windows.Forms.Button getData; private System.Windows.Forms.DataGrid dataGrid; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}
</source>