如何避免DatagridView C#中的红十字 [英] How to avoid Red Cross in DatagridView C#

查看:101
本文介绍了如何避免DatagridView C#中的红十字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用gridview定期更新值以及自动更新(基于autorefresh定时器)



我的问题是因为我在gridview上得到了一个大红十字会。

我用Google搜索并搜索代码以覆盖OnPaint Event。但是我的数据网格视图无法避免红十字会。



我尝试创建一个Datagridview而不是我的



Hi,
I am using a gridview which updates values periodically as well as automatically(based on autorefresh timer)

My Problem is that I get a Big Red Cross over my gridview.
I googled out and searched a code to override OnPaint Event. But It fails to avoid
the Red Cross over my datagridview.

I tried creating a Datagridview instead of mine

public class SafeDataGridView : DataGridView
{
  protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  {
    try
    {
      base.OnPaint(e);
    }
    catch (Exception)
    {
      this.Invalidate();
    }
  }
}





我附上了一个链接,显示了我的示例图片问题

http://koenaerts.ca/wp-content/uploads/dgvredx.png



请帮助我摆脱这个问题



I have attached a link which shows the sample picture of my problem
http://koenaerts.ca/wp-content/uploads/dgvredx.png

Please help Me to get rid of this problem

推荐答案

我想,这个是一个对角红十字,仅在设计模式下显示,这意味着渲染无法在设计时执行。尝试注释掉渲染代码,你会看到。



您的代码示例有一个主要错误:致电从渲染代码中无效它实际上意味着无限重复渲染,无限递归。显然,你不知道它是如何工作的。请查看我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ]。



你明白了吗?请参阅以下更多答案:

捕获面板上的图纸 [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

在C#.net鼠标滚轮中缩放图像 [ ^ ]。



最后建议:不要过度使用设计师;这样做意味着许多愚蠢的手工工作,这很难维护。当设计师模式根本无法显示时,保护您在设计师下运行的代码免受此类灾难的影响,这是一种常见情况。您可以在设计模式下尽可能少地执行有问题的代码,这可以通过使用此检查来实现:

I guess, this is a diagonal red cross showing just in the design mode, meaning that the rendering cannot be performed at design time. Try to comment out rendering code, and you will see.

Your code sample has one major bug: call to Invalidate from the rendering code. It actually means "infinite" repetition of rendering, infinite recursion. Apparently, you have no clue how it works. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

Are you getting the idea? See some more of those answers:
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

Final advice: don't overuse the designer; doing so means a lot of dumb manual work, which is hard to maintain. And protect your code running under the designer from such disasters when a designer mode cannot show at all, which is a common case. You can execute as little of questionable code in design mode as possible, which can be achieved by using this check:
if (!DesignMode)
   // do something which may fail



请参阅: https://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode(v = vs.110).aspx [ ^ ]。br />


-SA


Please see: https://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode(v=vs.110).aspx[^].

—SA


这篇关于如何避免DatagridView C#中的红十字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆