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

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 DataSourceDataTable : 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 DataSourceDataTable ( )
  19. {
  20. this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
  21. this.ClientSize = new System.Drawing.Size (464, 253);
  22. this.Text = "03_DataSourceDataTable" ;
  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 select = "SELECT * FROM products" ;
  49. SqlConnection con = new SqlConnection ( Login.Connection ) ;
  50. SqlDataAdapter da = new SqlDataAdapter ( select , con ) ;
  51. DataSet ds = new DataSet ( ) ;
  52. da.Fill ( ds , "Products" ) ;
  53. dataGrid.SetDataBinding ( ds , "Products" ) ;
  54. }
  55. /// <summary>
  56. /// Display the application window
  57. /// </summary>
  58. static void Main() 
  59. {
  60. Application.Run(new DataSourceDataTable());
  61. }
  62. }