using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class WorkerForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnEdit;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnExit;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private WindowsApplication1.WorkerDataSet workerDataSet1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public WorkerForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.workerDataSet1 = new WindowsApplication1.WorkerDataSet();
this.btnSave = new System.Windows.Forms.Button();
this.btnEdit = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.workerDataSet1)).BeginInit();
this.SuspendLayout();
// 
// label1
// 
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(104, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(424, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Click on the Edit Botton to load the records";
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "tblWorker";
this.dataGrid1.DataSource = this.workerDataSet1;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(72, 80);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(504, 136);
this.dataGrid1.TabIndex = 1;
this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
// 
// workerDataSet1
//

解决方案 »

  1.   

    this.workerDataSet1.DataSetName = "WorkerDataSet";
    this.workerDataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
    // 
    // btnSave
    // 
    this.btnSave.Location = new System.Drawing.Point(64, 248);
    this.btnSave.Name = "btnSave";
    this.btnSave.TabIndex = 2;
    this.btnSave.Text = "&Save";
    // 
    // btnEdit
    // 
    this.btnEdit.Location = new System.Drawing.Point(184, 248);
    this.btnEdit.Name = "btnEdit";
    this.btnEdit.TabIndex = 3;
    this.btnEdit.Text = "&Edit";
    this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
    // 
    // btnCancel
    // 
    this.btnCancel.Location = new System.Drawing.Point(312, 248);
    this.btnCancel.Name = "btnCancel";
    this.btnCancel.TabIndex = 4;
    this.btnCancel.Text = "&Cancel";
    // 
    // btnExit
    // 
    this.btnExit.Location = new System.Drawing.Point(448, 248);
    this.btnExit.Name = "btnExit";
    this.btnExit.TabIndex = 5;
    this.btnExit.Text = "E&xit";
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "tblWorker", new System.Data.Common.DataColumnMapping[] {
       new System.Data.Common.DataColumnMapping("WorkerID", "WorkerID"),
       new System.Data.Common.DataColumnMapping("Name", "Name")})});
    this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
    this.sqlDataAdapter1.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter1_RowUpdated);
    // 
    // sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = "DELETE FROM tblWorker WHERE (WorkerID = @Original_WorkerID) AND (Name = @Original" +
    "_Name OR @Original_Name IS NULL AND Name IS NULL)";
    this.sqlDeleteCommand1.Connection = this.sqlConnection1;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_WorkerID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "WorkerID", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Original, null));
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=ASKA;packet size=4096;user id=sa;data source=\"(local)\";persist sec" +
    "urity info=False;initial catalog=CMS";
    this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO tblWorker(WorkerID, Name) VALUES (@WorkerID, @Name); SELECT WorkerID," +
    " Name FROM tblWorker WHERE (WorkerID = @WorkerID)";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@WorkerID", System.Data.SqlDbType.Int, 4, "WorkerID"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, "Name"));
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT WorkerID, Name FROM tblWorker";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = "UPDATE tblWorker SET WorkerID = @WorkerID, Name = @Name WHERE (WorkerID = @Origin" +
    "al_WorkerID) AND (Name = @Original_Name OR @Original_Name IS NULL AND Name IS NU" +
    "LL); SELECT WorkerID, Name FROM tblWorker WHERE (WorkerID = @WorkerID)";
    this.sqlUpdateCommand1.Connection = this.sqlConnection1;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@WorkerID", System.Data.SqlDbType.Int, 4, "WorkerID"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, "Name"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_WorkerID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "WorkerID", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Original, null));
    // 
    // WorkerForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(632, 342);
    this.Controls.Add(this.btnExit);
    this.Controls.Add(this.btnCancel);
    this.Controls.Add(this.btnEdit);
    this.Controls.Add(this.btnSave);
    this.Controls.Add(this.dataGrid1);
    this.Controls.Add(this.label1);
    this.Name = "WorkerForm";
    this.Text = "WorkerForm";
    this.Load += new System.EventHandler(this.WorkerForm_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.workerDataSet1)).EndInit();
    this.ResumeLayout(false); }
    #endregion private void WorkerForm_Load(object sender, System.EventArgs e)
    {

    } private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
    {

    } private void btnEdit_Click(object sender, System.EventArgs e)
    {
    workerDataSet1.Clear();
    sqlDataAdapter1.Fill(workerDataSet1);
    } private void sqlDataAdapter1_RowUpdated(object sender, System.Data.SqlClient.SqlRowUpdatedEventArgs e)
    {

    } private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
    {

    }
    }
    }
      

  2.   

    实际上就是我建立了一个datagrid  想把dataset中的内容显示出来,而且是照书上抄的。