DataSourceDataViewManager.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:3k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. /// <summary>
  6. /// This class provides an example of creating and using a data grid.
  7. /// </summary>
  8. public class DataSourceDataViewManager : System.Windows.Forms.Form
  9. {
  10. private System.Windows.Forms.Button retrieveButton ;
  11. private System.Windows.Forms.DataGrid dataGrid ;
  12. /// <summary>
  13. /// Construct the window.
  14. /// </summary>
  15. /// <remarks>
  16. /// This method constructs the window by creating both the data grid and the button.
  17. /// </remarks>
  18. public DataSourceDataViewManager ( )
  19. {
  20. this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
  21. this.ClientSize = new System.Drawing.Size (464, 253);
  22. this.Text = "06_DataSourceDataViewManager" ;
  23. this.dataGrid = new System.Windows.Forms.DataGrid ();
  24. dataGrid.BeginInit ();
  25. dataGrid.Location = new System.Drawing.Point (8, 8);
  26. dataGrid.Size = new System.Drawing.Size (448, 208);
  27. dataGrid.TabIndex = 0;
  28. dataGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right ;
  29. this.Controls.Add (this.dataGrid);
  30. dataGrid.EndInit ();
  31. this.retrieveButton = new System.Windows.Forms.Button ();
  32. retrieveButton.Location = new System.Drawing.Point (384, 224);
  33. retrieveButton.Size = new System.Drawing.Size (75, 23);
  34. retrieveButton.TabIndex = 1;
  35. retrieveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right ;
  36. retrieveButton.Text = "Retrieve";
  37. retrieveButton.Click += new System.EventHandler (this.retrieveButton_Click);
  38. this.Controls.Add (this.retrieveButton);
  39. }
  40. /// <summary>
  41. /// Retrieve the data
  42. /// </summary>
  43. /// <param name="sender"> </param>
  44. /// <param name="e"> </param>
  45. protected void retrieveButton_Click (object sender, System.EventArgs e)
  46. {
  47. retrieveButton.Enabled = false ;
  48. string orders = "SELECT * FROM Orders";
  49. string customers = "SELECT * FROM Customers";
  50. SqlConnection con = new SqlConnection ( Login.Connection ) ;
  51. SqlDataAdapter da = new SqlDataAdapter ( orders , con ) ;
  52. DataSet ds = new DataSet ( ) ;
  53. da.Fill ( ds , "Orders" ) ;
  54. da = new SqlDataAdapter ( customers , con ) ;
  55. da.Fill ( ds , "Customers" ) ;
  56. ds.Relations.Add("CustomerOrders",
  57. ds.Tables["Customers"].Columns["CustomerID"],
  58. ds.Tables["Orders"].Columns["CustomerID"]);
  59. DataViewManager dvm = new DataViewManager ( ds ) ;
  60. dvm.DataViewSettings["Customers"].RowFilter = "Country='UK'" ;
  61. dataGrid.SetDataBinding ( dvm , "Customers" ) ;
  62. }
  63. /// <summary>
  64. /// Display the application window
  65. /// </summary>
  66. static void Main() 
  67. {
  68. Application.Run(new DataSourceDataViewManager());
  69. }
  70. }