这是我 写的测试程序 请高手帮忙解决using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace test
{
public partial class Form1 : Form
{
/// <summary>
/// 循环线程
/// </summary>
private void ManageThreads()
{
Thread t1 = new Thread(new ThreadStart(InsertTB));
t1.IsBackground = true;
t1.Start();
} private void InsertTB()
{
for (int i = 0; i < 3000; i++)
{
DataRow row = logDT.NewRow();
foreach (DataColumn column in logDT.Columns)
{
row[column] = column.ColumnName + i.ToString();
}
logDT.Rows.InsertAt(row, 0);
}
}
public Form1()
{
InitializeComponent();
} DataTable logDT = new DataTable(); private void button1_Click(object sender, EventArgs e)
{
logDT.Columns.Add("a");
logDT.Columns.Add("b");
logDT.Columns.Add("c");
logDT.Columns.Add("d");
logDT.Columns.Add("e");
logDT.Columns.Add("f");
DataGridView logDGV = new DataGridView();
logDGV.Dock = System.Windows.Forms.DockStyle.Bottom;
logDGV.Height = 100;
logDGV.ScrollBars = System.Windows.Forms.ScrollBars.Both;
logDGV.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
logDGV.ReadOnly = true;
logDGV.DataSource = logDT;
logDGV.Parent = this.FindForm(); Control.CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(new ThreadStart(ManageThreads));
thread.Start();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace test
{
public partial class Form1 : Form
{
/// <summary>
/// 循环线程
/// </summary>
private void ManageThreads()
{
Thread t1 = new Thread(new ThreadStart(InsertTB));
t1.IsBackground = true;
t1.Start();
} private void InsertTB()
{
for (int i = 0; i < 3000; i++)
{
DataRow row = logDT.NewRow();
foreach (DataColumn column in logDT.Columns)
{
row[column] = column.ColumnName + i.ToString();
}
logDT.Rows.InsertAt(row, 0);
}
}
public Form1()
{
InitializeComponent();
} DataTable logDT = new DataTable(); private void button1_Click(object sender, EventArgs e)
{
logDT.Columns.Add("a");
logDT.Columns.Add("b");
logDT.Columns.Add("c");
logDT.Columns.Add("d");
logDT.Columns.Add("e");
logDT.Columns.Add("f");
DataGridView logDGV = new DataGridView();
logDGV.Dock = System.Windows.Forms.DockStyle.Bottom;
logDGV.Height = 100;
logDGV.ScrollBars = System.Windows.Forms.ScrollBars.Both;
logDGV.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
logDGV.ReadOnly = true;
logDGV.DataSource = logDT;
logDGV.Parent = this.FindForm(); Control.CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(new ThreadStart(ManageThreads));
thread.Start();
}
}
}
logDGV.Dock = System.Windows.Forms.DockStyle.Bottom;
logDGV.Height = 100;
logDGV.ScrollBars = System.Windows.Forms.ScrollBars.Both;
logDGV.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
logDGV.ReadOnly = true;
//logDGV.DataSource = logDT;
logDGV.Parent = this.FindForm();
Control.CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(new ThreadStart(ManageThreads));
thread.Start();
这样还是不行啊 logDGV.DataSource = logDT;
还有一处:把
public Form1()
{
InitializeComponent();
}
放最前边
这两处都影响。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} /// <summary>
/// 循环线程
/// </summary>
private void ManageThreads()
{
Thread t1 = new Thread(new ThreadStart(InsertTB));
t1.IsBackground = true;
t1.Start();
} private void InsertTB()
{
for (int i = 0; i < 100; i++)
{
DataRow row = logDT.NewRow();
foreach (DataColumn column in logDT.Columns)
{
row[column] = column.ColumnName + i.ToString();
}
logDT.Rows.InsertAt(row, 0);
}
}
DataTable logDT = new DataTable(); private void button1_Click(object sender, EventArgs e)
{
logDT.Columns.Add("a");
logDT.Columns.Add("b");
logDT.Columns.Add("c");
logDT.Columns.Add("d");
logDT.Columns.Add("e");
logDT.Columns.Add("f"); DataGridView logDGV = new DataGridView();
logDGV.Dock = System.Windows.Forms.DockStyle.Bottom;
logDGV.Height = 100;
logDGV.ScrollBars = System.Windows.Forms.ScrollBars.Both;
logDGV.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
logDGV.ReadOnly = true;
//logDGV.DataSource = logDT;
logDGV.Parent = this.FindForm();
Control.CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(new ThreadStart(ManageThreads));
thread.Start();
logDGV.DataSource = logDT;
}
}
}
namespace test
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(194, 144);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1;
}
}
6楼的两个文件分别是: Form1.cs 和 Form1.Designer.cs. 把Form1.resx删除生成菜单 --> 清理解决方案 --> 重新生成