首页 新闻 会员 周边

WinForm 线程加载dataGridView时候 报错

0
悬赏园豆:100 [已解决问题] 解决于 2016-11-07 09:07

当界面数据刚打开加载数据的时候,如果鼠标正好停在dataGridView内有数据的一行,就会报错。。。有大神看到感兴趣的回复一下,我会追加详细内容。。感谢感谢

问题补充:

try 了之后,进入页面会报错:创建句柄错误。。。老哥求解。。

 

MethodInvoker mi = new MethodInvoker(GetData);
BeginInvoke(mi);

如果加了这个委托,加载数据是不会错了,但是加载等待的那个圈圈效果就没了

 

 

        private bool LoadState
        {
            set
            {
                if (value)
                {
                    panel1.Visible = true;
                }
                else
                {
                    panel1.Visible = false;
                    circularProgress1.Value = 0;
                }
            }
        }

        private void Init_Tcir()
        {
            tcir = new System.Windows.Forms.Timer();
            tcir.Enabled = true;
            tcir.Interval = 10;
            tcir.Tick += Tcir_Tick;
        }
        private void Tcir_Tick(object sender, EventArgs e)
        {
            if (circularProgress1.Value == 100)
            {
                circularProgress1.Value = 0;
            }
            else
            {
                circularProgress1.Value += 2;
            }
        }
public void GetData()
        {
            LoadState=true;
BaseInfo baseInfo = OrderFunction.GetTSOrderOnDay((lbl_time.Text), page, keyword, states);
dataGridViewX1.DataSource = listorder.OrderByDescending(x => x.CreateTime).Take(10).ToList();
LoadState = false;
}
        private void LoadData()
        {
            Thread t = new Thread(new ThreadStart(GetData));
            t.Start();
        }

        private void TSOrderForm_Load(object sender, EventArgs e)
        {

                CanCellPaint = true;
                dataGridViewX1.AutoGenerateColumns = false;
                dataGridViewX1.Focus();
                lbl_time.Text = DateTime.Now.Date.ToShortDateString();
                dataGridViewX1.RowTemplate.Height = 26;
                dataGridViewX1.Height = 287;
                dataGridViewX1.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
                Init_Tcir();//加载效果附加
                LoadData();
            //Application.DoEvents();
            //MethodInvoker mi = new MethodInvoker(GetData);
            //BeginInvoke(mi);
            
        }

System.Windows.Forms.Timer tcir = new System.Windows.Forms.Timer();

差不多基本代码就这个了,望大神们多点评一下

 

panel1是包裹住转圈控件的容器。。先声明一下

public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
#region 加载效果附加
/// <summary>
/// 加载控件
/// </summary>
public bool LoadState
{
set
{
if (value)
{
panel1.Visible = true;
}
else
{
panel1.Visible = false;
circularProgress1.Value = 0;
}
}
}
System.Windows.Forms.Timer tcir = new System.Windows.Forms.Timer();
private void Init_Tcir()
{
tcir = new System.Windows.Forms.Timer();
tcir.Enabled = true;
tcir.Interval = 10;
tcir.Tick += Tcir_Tick;
}
private void Tcir_Tick(object sender, EventArgs e)
{
if (circularProgress1.Value == 100)
{
circularProgress1.Value = 0;
}
else
{
circularProgress1.Value += 2;
}
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
Init_Tcir();
LoadData();
}
private void LoadData()
{
Thread t = new Thread(new ThreadStart(GetData));
t.Start();
}
public void GetData()
{
LoadState = true;

#region 执行
Thread.Sleep(2000);
dataGridViewX1.DataSource = null;

List<string> a = new List<string>() { "asdasd", "asdasd", "asdasd", "asdasd", "asdasd", "asdasd" };
dataGridViewX1.DataSource = a;//这里是一个获取数据源的方法,因为的调用接口,网络差时会有1-2秒延迟


#endregion

LoadState = false;
}

这就是源代码模板了,ui界面就是一个dataGridView,一个被panel包裹住的circularProgress1

JO的美好时光的主页 JO的美好时光 | 初学一级 | 园豆:14
提问于:2016-11-03 16:43
< >
分享
最佳答案
0

报错很正常,你加一个try catch 就解决了

收获园豆:100
刘宏玺 | 专家六级 |园豆:14020 | 2016-11-03 16:52

老哥,不能这么玩吧,tyr了就跳转不了页面了。...

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 16:52

@JO的美好时光: 把try加在你转圈圈的那里

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 16:54

Control.CheckForIllegalCrossThreadCalls = false;

在load中加入这句话试试

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 16:56

@刘宏玺: 老哥,这个我试过,和圈圈没什么关系,应该是线程之间的一些协议我没弄清楚。。。

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 16:56

@刘宏玺: 

InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

之前有了。。还是有问题

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 16:59

@JO的美好时光: 

用MethodInvoker方法的时候在转圈圈的方法下面加入下面的代码强制刷新窗体

Application.DoEvent();

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 17:00

@刘宏玺: 老哥,顺序是这样的 先加载转圈圈的效果,然后获取数据,然后停止转圈,因为刚打开页面首次加载会有点慢,按照你刚才说的做了,还是没效果。。。。

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:03

@JO的美好时光: 我是这样理解的,转圈圈是窗体线程控制的,获取数据是另一个线程控制的,我说的对吗?

如果对的话你就别执行停止转圈,看下最后会不会出现转圈的效果

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 17:07

@刘宏玺: 老哥,代码我贴出来了,可能是我表达不太准确,帮忙看下这个逻辑有没有错

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:08

@JO的美好时光: 我知道什么问题了,你这样做

 

        private void Tcir_Tick(object sender, EventArgs e)
        {
            if (circularProgress1.Value == 100)
            {
                circularProgress1.Value = 0;
            }
            else
            {
                circularProgress1.Value += 2;
            }
            Application.DoEvent();//加入这行
        }    

然后在使用委托

MethodInvoker mi = new MethodInvoker(GetData);

BeginInvoke(mi);

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 17:17

@刘宏玺: 老哥,还是不行阿。想要它不出错的执行很简单,用委托就好了,但是load事件里面的Init_Tcir();//加载效果附加 就没用了。。

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:20

@JO的美好时光: 你在Tcir_Tick中设置下断点,看看执行进来没?

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 17:24

@刘宏玺: 

在 System.Windows.Forms.DataGridViewCell.GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle, Int32 rowIndex, Boolean includeColors)
在 System.Windows.Forms.DataGridViewCell.GetPreferredHeight(Int32 rowIndex, Int32 width)
在 System.Windows.Forms.DataGridViewCell.OnCellDataAreaMouseEnterInternal(Int32 rowIndex)
在 System.Windows.Forms.DataGridViewCell.OnMouseMoveInternal(DataGridViewCellMouseEventArgs e)
在 System.Windows.Forms.DataGridView.OnCellMouseMove(DataGridViewCellMouseEventArgs e)
在 System.Windows.Forms.DataGridView.UpdateMouseEnteredCell(HitTestInfo hti, MouseEventArgs e)
在 System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseMove(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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.RunDialog(Form form)
在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
在 System.Windows.Forms.Form.ShowDialog()
在 KingComeWaiter.More.MoreMain.btn_ts_Click(Object sender, EventArgs e)
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 DevComponents.DotNetBar.ButtonX.OnClick(EventArgs e)
在 DevComponents.DotNetBar.ButtonX.OnMouseUp(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 DevComponents.DotNetBar.PopupItemControl.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.DoEvents()
在 KingComeWaiter.More.TSOrderForm.Tcir_Tick(Object sender, EventArgs e)
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.RunDialog(Form form)
在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
在 System.Windows.Forms.Form.ShowDialog()
在 KingComeWaiter.MainForm.buttonX11_Click(Object sender, EventArgs e)
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 DevComponents.DotNetBar.ButtonX.OnClick(EventArgs e)
在 DevComponents.DotNetBar.ButtonX.OnMouseUp(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 DevComponents.DotNetBar.PopupItemControl.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 KingComeWaiter.Program.Main(String[] AutoLogin)
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()

 

老哥,这是报错的内容,我看不出问题所在。。

按照你说的执行后程序是正常运行的,断点后是能进去的,但还是加载效果的圈圈不出来

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:27

@JO的美好时光: 好吧,我发现我考虑错了

Application.DoEvent();放置在这里,应该就没问题了,你先试试,我下班了,可能回复要慢了

private bool LoadState
        {
            set
            {
                if (value)
                {
                    panel1.Visible = true;
                }
                else
                {
                    panel1.Visible = false;
                    circularProgress1.Value = 0;
                }
            Application.DoEvent();//加再这里,因为你这个属性是多线程调用的,可能主线程监控不到控件属性变化, 你强制窗体刷新,应该能够实现!
            }
        }
刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 17:35

@刘宏玺: 加载转圈事件是没问题的,在加载数据(有数据 且还未加载出来)的时候鼠标正好停在DataGridView里面,而且停留的位子是有数据的那一行,就会有以上的那个问题,有时的index索引超出范围,有时是未将对象引用到实例。

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:35

@JO的美好时光: 你是不是使用了mouse的事件?使用了的话就在使用的地方try一下,不然不应该会报错的!

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-03 17:36

@刘宏玺: dataGridViewX1_CellClick   dataGridViewX1_CellPainting  只用了这两个事件,老哥你下班就先回去吧,有空在帮忙解答一下就好啦

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:39

@JO的美好时光: 你在dataGridViewX1_CellClick   dataGridViewX1_CellPainting 这两个事件中使用try不就实现了嘛!

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-04 09:31

@刘宏玺: 没用。。。还没到里面就挂了

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-04 16:35

@JO的美好时光: 你还是把报错的地方截个图给我看下把,包括代码和错误信息,我现在都不知道是什么地方报错了

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-04 16:36

@刘宏玺: 我上面有一个demo代码,老哥可以试一下,因为它报错有时会不一样。。

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-04 17:37

@JO的美好时光: 你的报错的demo我实验过了,确实会出现问题,我给你一个不会出问题的demo把,就是用你的demo改的,使用backgroundWorker组件实现的,拖进来就可以使用,你试一下效果

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }
        #region 加载效果附加
        /// <summary>
        /// 加载控件
        /// </summary>
        public bool LoadState
        {
            set
            {
                if (value)
                {
                    panel1.Visible = true;
                }
                else
                {
                    panel1.Visible = false;
                    circularProgress1.Value = 0;
                }
            }
        }
        System.Windows.Forms.Timer tcir = new System.Windows.Forms.Timer();
        private void Init_Tcir()
        {
            tcir = new System.Windows.Forms.Timer();
            tcir.Enabled = true;
            tcir.Interval = 10;
            tcir.Tick += Tcir_Tick;
        }
        private void Tcir_Tick(object sender, EventArgs e)
        {
            if (circularProgress1.Value == 100)
            {
                circularProgress1.Value = 0;
            }
            else
            {
                circularProgress1.Value += 2;
            }
        }
        #endregion
        private void Form1_Load(object sender, EventArgs e)
        {
            Init_Tcir();
            LoadState = true;
            backgroundWorker1.RunWorkerAsync();
        }
        List<string> a = null;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(2000);//这里是一个获取数据源的方法,因为的调用接口,网络差时会有1-2秒延迟
            a = new List<string>() { "asdasd", "asdasd", "asdasd", "asdasd", "asdasd", "asdasd" };
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dataGridViewX1.DataSource = null;
            dataGridViewX1.DataSource = a;
            LoadState = false;
        }
    }
}

标黄的两个就是这个控件的基本事件了,你好好研究下!

刘宏玺 | 园豆:14020 (专家六级) | 2016-11-06 18:26

@刘宏玺: 完美解决,感谢老哥,结帖!

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-07 09:04
其他回答(3)
0

这种事件驱动模式视图界面层原则上就应该Invoke,建议数据Thread,界面Invoke(不然真是怎么死,什么时间死你都可能不知道),如果觉得卡建议用楼上Application.DoEvent(),如果还卡~~我认为软件设计很有问题或者该换控件了。(你能看到这种集合控件的绘制也是提供了Begin【没记错词语的话】,这种模式在数据大的时候也是可以提高速率的)。总得来说这个控件太庞大粗鲁了,数据大很劣势,而且不友好。

花飘水流兮 | 园豆:13560 (专家六级) | 2016-11-03 17:22

谢老哥提点,其实这个数据的做了分页的,调用接口一次才查10条数据

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 17:23
0

你所有非ui线程对ui的操作全部都改掉吧。

void Process(Control ctl,Action<Control> action)

{

  if(ctl.InvokeRequired)

   ctl.Invoke(action);

else

  action();

}

...//

private void Tcir_Tick(object sender, EventArgs e)

{

Process(circularProgress1,()=>{

if (circularProgress1.Value == 100)

{ circularProgress1.Value = 0; }

else { circularProgress1.Value += 2; }

}

});

 

LoadState的set方法及其他control的任何变更也做同样变更

另你timer能否换成别的?winform我不太清楚,感觉你这里可能是在timer的周期里更改progressbar的时候但这个时候控件是不可见导致的。

Daniel Cai | 园豆:10424 (专家六级) | 2016-11-03 18:08

  谢老哥提点,不过你这个我不太回应。。小弟初涉。。

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-03 18:18
0

贴下 dataGridViewX1_CellPainting 代码

jello chen | 园豆:7306 (大侠五级) | 2016-11-03 20:55

刚上班 

    private void dataGridViewX1_CellPainting_1(object sender, DataGridViewCellPaintingEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 7)
                {
                    if (e.Value + "" == "待接单")
                    {
                        var a = sender;
                        this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.FromArgb(255, 145, 0);
                    }
                    else if (e.Value + "" == "已接单")
                    {
                        var a = sender;
                        this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.FromArgb(0, 158, 16);
                    }
                    else if (e.Value + "" == "已取消")
                    {
                        var a = sender;
                        this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.FromArgb(176, 176, 176);
                    }
                }
                if (e.ColumnIndex == 5 && e.RowIndex >= 0 && listorder.OrderByDescending(x => x.CreateTime).ToList().Count > 0)
                {
                    this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = listorder.OrderByDescending(x => x.CreateTime).ToList()[e.RowIndex].OrderDetailList.Sum(x => x.GoodsNumber);
                }
                else if (e.ColumnIndex == 0 && e.RowIndex >= 0)
                {
                    this.dataGridViewX1.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1 + ( page - 1 ) * 10;
                }
            }
            catch (Exception ex)
            {

            }
        }

代码就这个了

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-04 08:39

@JO的美好时光: 可以这样排查,先把Cell相关的所有事件监听去掉,比如你这里的Cell Click,Cell Painting,看看有没有你说的这个问题

支持(0) 反对(0) jello chen | 园豆:7306 (大侠五级) | 2016-11-04 10:38

@jello chen: 老哥你说的这个我也试过,好像没用。。。

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-04 10:39

@JO的美好时光: 方便写个简单的demo复现一下这个问题不,上传到博客园

支持(0) 反对(0) jello chen | 园豆:7306 (大侠五级) | 2016-11-04 11:33

@jello chen: 代码上面贴了阿老哥,省去了一两个获取数据接口的方法而已。。

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-04 16:08

@jello chen: demo代码贴出来了,麻烦老哥指点一下

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-04 16:28

@JO的美好时光: https://i.cnblogs.com/Files.aspx在这里上传,然后把链接贴出来

支持(0) 反对(0) jello chen | 园豆:7306 (大侠五级) | 2016-11-04 17:26

@jello chen: 有这么麻烦吗.....

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-05 09:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册