protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.dataGrid2 = new System.Windows.Forms.DataGrid();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(32, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(640, 152);
this.dataGrid1.TabIndex = 0;
// 
// groupBox1
// 
this.groupBox1.Controls.Add(this.dataGrid2);
this.groupBox1.Location = new System.Drawing.Point(16, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(672, 184);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
// 
// dataGrid2
// 
this.dataGrid2.DataMember = "";
this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid2.Location = new System.Drawing.Point(24, 24);
this.dataGrid2.Name = "dataGrid2";
this.dataGrid2.Size = new System.Drawing.Size(624, 144);
this.dataGrid2.TabIndex = 0;
this.dataGrid2.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid2_Navigate);
// 
// groupBox2
// 
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Controls.Add(this.comboBox1);
this.groupBox2.Controls.Add(this.textBox2);
this.groupBox2.Controls.Add(this.textBox1);
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(16, 216);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(672, 72);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "groupBox2";
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(352, 40);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 24);
this.button2.TabIndex = 7;
this.button2.Text = "添加列";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(216, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 6;
this.button1.Text = "创建表";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// comboBox1
// 
this.comboBox1.Items.AddRange(new object[] {
   "char",
   "Int32\t",
   "Text"});
this.comboBox1.Location = new System.Drawing.Point(96, 48);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(96, 20);
this.comboBox1.TabIndex = 5;
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(392, 16);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(104, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(96, 17);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
// 
// label3
// 
this.label3.Location = new System.Drawing.Point(272, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 16);
this.label3.TabIndex = 2;
this.label3.Text = "第二列后的列名";
// 
// label2
// 
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 1;
this.label2.Text = "类型";
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(16, 17);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 24);
this.label1.TabIndex = 0;
this.label1.Text = "表名称";
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(24, 296);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.TabIndex = 3;
this.button3.Text = "查看表";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// button4
// 
this.button4.Location = new System.Drawing.Point(168, 296);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.TabIndex = 4;
this.button4.Text = "添加行";
this.button4.Click += new System.EventHandler(this.button4_Click);
// 
// button5
// 
this.button5.Location = new System.Drawing.Point(320, 296);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(80, 24);
this.button5.TabIndex = 5;
this.button5.Text = "删除列";
this.button5.Click += new System.EventHandler(this.button5_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(704, 342);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void dataGrid2_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{

} private void button1_Click(object sender, System.EventArgs e)
{
try
{
table=new DataTable(textBox2.Text);}
catch{MessageBox.Show("请输入表的名称!");}
}

解决方案 »

  1.   

    private void button2_Click(object sender, System.EventArgs e)
    {
    try
    {
    col=new DataColumn();
    if(comboBox1.Text=="char")
    {
    col.DataType=System.Type.GetType("System.Char");
    }
    if(comboBox1.Text=="Int32")
    {
    col.DataType=System.Type.GetType("System.Int32");
    }
    if(comboBox1.Text=="Text")
    {
    col.DataType=System.Type.GetType("System.String");
    }
    if(i==0)
    {
    col.ColumnName="ID";
    col.AutoIncrement=true;
    col.Unique=true;
    MessageBox.Show("现在创建第一列,系统自动产生.");
    }
    else
    {
    col.ColumnName=textBox1.Text;
    col.AutoIncrement=false;
    col.Unique=false;
    int j=i+1;
    MessageBox.Show("现在创建第"+j.ToString()+"列");
    }
    col.ReadOnly=false;
    table.Columns.Add(col);
    i++;
    }
    catch{MessageBox.Show("列名重复,无效!");}
    } private void button4_Click(object sender, System.EventArgs e)
    {
    row=table.NewRow();
    table.Rows.Add(row); } private void button5_Click(object sender, System.EventArgs e)
    {
    table.Columns.Remove(textBox1.Text);
    } private void button3_Click(object sender, System.EventArgs e)
    {
    dataGrid1.DataSource=table;
    } }
    }
    }