Form1.cs
Upload User: yiyuerguo
Upload Date: 2014-09-27
Package Size: 3781k
Code Size: 4k
Category:

CSharp

Development Platform:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.OleDb;
  8. namespace useDataGrid
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.DataGrid dataGrid1;
  16. private System.Data.DataSet ds;
  17. private OleDbDataAdapter da;
  18. private OleDbCommandBuilder cb;
  19. /// <summary>
  20. /// Required designer variable.
  21. /// </summary>
  22. private System.ComponentModel.Container components = null;
  23. public Form1()
  24. {
  25. //
  26. // Required for Windows Form Designer support
  27. //
  28. InitializeComponent();
  29. //
  30. // TODO: Add any constructor code after InitializeComponent call
  31. //
  32. }
  33. /// <summary>
  34. /// Clean up any resources being used.
  35. /// </summary>
  36. protected override void Dispose( bool disposing )
  37. {
  38. if( disposing )
  39. {
  40. if (components != null) 
  41. {
  42. components.Dispose();
  43. }
  44. }
  45. base.Dispose( disposing );
  46. }
  47. #region Windows Form Designer generated code
  48. /// <summary>
  49. /// Required method for Designer support - do not modify
  50. /// the contents of this method with the code editor.
  51. /// </summary>
  52. private void InitializeComponent()
  53. {
  54. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  55. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  56. this.SuspendLayout();
  57. // 
  58. // dataGrid1
  59. // 
  60. this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  61. | System.Windows.Forms.AnchorStyles.Right)));
  62. this.dataGrid1.DataMember = "";
  63. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  64. this.dataGrid1.Location = new System.Drawing.Point(8, 8);
  65. this.dataGrid1.Name = "dataGrid1";
  66. this.dataGrid1.Size = new System.Drawing.Size(296, 216);
  67. this.dataGrid1.TabIndex = 0;
  68. // 
  69. // Form1
  70. // 
  71. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  72. this.ClientSize = new System.Drawing.Size(312, 269);
  73. this.Controls.Add(this.dataGrid1);
  74. this.Name = "Form1";
  75. this.Text = "使用DataGrid控件来显示数据";
  76. this.Load += new System.EventHandler(this.Form1_Load);
  77. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  78. this.ResumeLayout(false);
  79. }
  80. #endregion
  81. /// <summary>
  82. /// The main entry point for the application.
  83. /// </summary>
  84. [STAThread]
  85. static void Main() 
  86. {
  87. Application.Run(new Form1());
  88. }
  89.         private void Form1_Load(object sender, System.EventArgs e)
  90.         {
  91. //创建一个数据链接
  92. string strCon =@" Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =sample.xls;Extended Properties=Excel 8.0" ;
  93. OleDbConnection myConn = new OleDbConnection ( strCon ) ;
  94. string strCom = " SELECT * FROM [Sheet1$] " ;
  95. myConn.Open ( ) ;
  96. //打开数据链接,得到一个数据集
  97. OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
  98. //创建一个 DataSet对象
  99. ds = new DataSet ( ) ;
  100. //得到自己的DataSet对象
  101. myCommand.Fill (ds , "[Sheet1$]" ) ;
  102. //关闭此数据链接
  103. myConn.Close ( ) ;
  104.             
  105.             // 把数据显示在DataGrid控件中.
  106.             dataGrid1.DataSource = ds.DefaultViewManager; 
  107.              
  108.         }
  109. private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
  110. {
  111. }
  112. private void button1_Click(object sender, System.EventArgs e)
  113. {
  114. da.Update(ds,"Developer");
  115. }
  116. }
  117. }