这是我 写的测试程序  请高手帮忙解决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();
        }
    }
}

解决方案 »

  1.   

    调试了有30半小时,发现问题出在哪了.把 logDGV.DataSource = logDT; 放到 thread.Start(); 之后
      

  2.   

    数据源DataTable logDT 改变之后,需要重新绑定。初始完 DataTable logDT 之后,再绑定,效率高。
      

  3.   

     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;
      

  4.   

    下面代码运行正常.
    还有一处:把
    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;
        }
    }
      

  5.   

    上面说的两个地方,一定有影响,我试了一下,改回每一处,都是8楼那样的效果.
    6楼的两个文件分别是: Form1.cs 和 Form1.Designer.cs. 把Form1.resx删除生成菜单 --> 清理解决方案 --> 重新生成