首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

多线程+datagridview的有关问题

2013-06-19 
多线程+datagridview的问题看代码测试代码1:在datagridview里输出一个 1*10的数组,值为1~10.private void

多线程+datagridview的问题
看代码

测试代码1:在datagridview里输出一个 1*10的数组,值为1~10.


        private void fuc()
        {
            char[] tmpValue = "1,2,3,4,5,6,7,8,9,10".ToCharArray();
            string[] strs = (new string(tmpValue)).Split(',');

            DataTable dt = new DataTable("Data");
            DataRow dr;

            dr = dt.NewRow();
            for (int j = 0; j < strs.Length; j++)
            {
                dt.Columns.Add(new DataColumn(((char)('A' + j)).ToString(), typeof(double)));
                dr[j] = strs[j];
            }
            dt.Rows.Add(dr);

            dataGridView1.DataSource = dt;
        }


代码2:作用是 单击button2按钮,进行显示fuc操作。这个没有问题。(多次操作也没有问题)

        private void button2_Click(object sender, EventArgs e)
        {
            fuc();
        }


代码3:问题来了,当我用多线程调用fuc()函数的时候就会出现:第一次操作没有问题,但是点击第二次(包括第三第四……次)时就会提示错误。(错误已经在下面贴出)。

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("xx");//此时如果我将这段代码不注销,这个错误只是偶尔会出现。
            Thread thread = new Thread(fuc);
            thread.Start();
        }




错误提示:

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
   在 System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
   在 System.Windows.Forms.DataGridViewTextBoxCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)


   在 System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
   在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   在 System.Windows.Forms.Control.WmPaint(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.DataGridView.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)





求解!

[解决办法]
这是因为你从线程去操作datagridview的原因
[解决办法]
需要用委托、Invoke来解决,你可以再网上搜下“跨线程调用控件”。

热点排行