.NET Windows窗体DataGridView当程序添加时,单元文本消失 [英] .NET Windows forms DataGridView Cell text disappears when added programatically

查看:133
本文介绍了.NET Windows窗体DataGridView当程序添加时,单元文本消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发包含 DataGridView 的Windows窗体应用程序。这个 DataGridView 有3列,全部只是文本单元格:




  • 时间戳

  • 连接

  • code>消息



我遇到的问题是当我添加一行(以编程方式),我发现文本太长时间消失了。具体来说,如果文字长度超过 4563 个字符,则文字将消失。



我知道 DataGridViewTextBoxColumn 类有一个属性调用 MaxInputLength 可以限制输入的字符数。但是根据文档,它只影响用户手动输入的文本。但是,我以编程方式输入此文本。



只是为了确保,我将此属性设置得非常高,但是当我传递 4563 字符限制时,仍然会出现消失的文本问题。 / p>

我注意到的一件事是,文本仍然存在(即,底部的滚动条仍然可以滚动,就像文本仍然存在),但我看不到文本本身。我也可以编辑文字。



我可以添加字符,直到 4563 限制,但一旦通过,文本消失。如果我按退格键返回到正确的 4563 字符,文本将重新出现。



我正在使用.NET 4.0开发此文件,因为我必须支持Windows XP。

解决方案

这是一个简短的答案,可能会让你失望:这是一个报告的错误,由Microsoft验证,关闭为不够重要,不能解决。可能还有更多的实例,但至少从2011年开始就已经知道了这一点。。 解决方法只是限制单元格宽度的大小,但对于可能不令人满意的您来说。



然而,好奇心得到我最好的,所以我开始深入了解它;这是第一个值得一提的观察:





如果你看一系列的照片,你会注意到我复制了你的问题的默认字体大小/样式和具体号码5460.什么是特别的约5460?嗯,没有什么特别的,除了当你的角色阈值越过它,列的ContentBounds和宽度通过32767. 32767是什么特别的?除了作为 DataGridViewTextBoxCell 的默认 MaxInputLength ,它是有符号短语或Int16(2 ^ 15)的上限-1)。我非常怀疑这是一个巧合,这个问题在这里发生,虽然不是与$ code> MaxInputLength 本身有关的任何事情。我会打赌你首先注意到4563个字符的问题,因为您的字体大小也将宽度扩大到32767。



下一个问题是为什么?我不太确定我开始跟随兔子洞,并拆卸了一些.NET 4.0 DataGridView *库来找出。这是一个非常大而复杂的控件,我无法得出任何明确的结论,但有一件事我发现值得注意的是,一列可以假定为65536的绝对最大宽度,一个UNsigned Int16(2 ^ 16):





在添加或调整列的大小时,您可以在很多私人内部的位置看到此检查,并且我进行了测试。大小不会更大



这是有讽刺意义的两个原因。对于一个,使用默认设置,只能在列中显示10922个字符(每个字符65536个/ 6个像素),尽管编辑输入长度为32767个字符,并且以编程方式任意。



其次,为什么这个问题会根据最大宽度的列的符号变量的最大值开始裁剪?嗯这是一个猜测,但我认为,在任何地方,最大值的任何渲染文本被设置为一个常规短,而不是一个无符号短小...或这些线的东西。在 DataGridViewTextBoxCell()的实现中,我怀疑 PaintPrivate()方法,所以如果你感觉到活泼的,也许放一个显微镜。您将需要一个IL反汇编程序来查看这些未公开暴露的内容。具体来说,这部分代码我怀疑:

  if(text!= null&&(paint& &!flag2 || computeContentBounds))
{
int y = cellStyle.WrapMode == DataGridViewTriState.True? 1:2;
rectangle3.Offset(0,y);
// ISSUE:显式引用操作
// ISSUE:引用类型的变量
Rectangle& local = @ rectangle3;
// ISSUE:显式引用操作
int width =(^ local).Width;
// ISSUE:显式引用操作
(^ local).Width = width;
rectangle3.Height - = y + 1;
if(rectangle3.Width> 0&&& square3.Height> 0)
{
TextFormatFlags cellStyleAlignment = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(this.DataGridView.RightToLeftInternal,cellStyle.Alignment,cellStyle .WrapMode);
if(paint)
{
if(DataGridViewCell.PaintContentForeground(paintParts))
{
if((cellStyleAlignment& TextFormatFlags.SingleLine)!= TextFormatFlags.Default )
cellStyleAlignment | = TextFormatFlags.EndEllipsis;
TextRenderer.DrawText((IDeviceContext)graphics,text,cellStyle.Font,rectangle3,flag3?cellStyle.SelectionForeColor:cellStyle.ForeColor,cellStyleAlignment);
}
}
else
rectangle1 = DataGridViewUtilities.GetTextBounds(rectangle3,text,cellStyleAlignment,cellStyle);
}

对不起,这本书!



TL;如果您希望将字符包含在巨大细胞中,请使用小分支。


I am developing a Windows forms application that includes a DataGridView. This DataGridView has 3 columns, all of which are simply text cells:

  • Timestamp
  • Connection
  • Message

The issue I'm running into is that when I add a row (programmatically), I'm finding that the text disappears if it is too long. To be specific, if the text exceeds 4563 characters in length, then the text disappears.

I know that the DataGridViewTextBoxColumn class has a property call MaxInputLength that can limit the number of characters entered. But according the the Documentation, it only affects text that is input manually by the user. I, however, am inputting this text programmatically.

Just to make sure though, I set this property very high but the disappearing text issue still arises when I pass the 4563 character limit.

One thing I have noticed is that the text is still there (i.e. the scroll bar along the bottom can still be scrolled as though the text is still there) but I cannot see the text itself. I can also edit the text.

I can add characters until the 4563 limit but as soon as I pass that, the text disappears. If I press backspace to return to exactly 4563 characters, the text reappears.

I am developing this using .NET 4.0, since I have to support Windows XP.

解决方案

Here's the short answer that will probably disappoint you: It's a reported bug and verified by Microsoft, closed as "Not important enough to fix". There may be more instances of it, but it's been known since at least 2011 DataGridView control shows blank cell if large string is entered and column resized to max. The "workaround" is to just limit the size of the cells width, but for you that may not be satisfactory.

However, curiosity got the best of me so I started looking into it a little deeper; Here's the first observation worth mentioning:

If you look at the series of pictures, you'll notice I replicated your problem with the default font size/style and the specific number 5460. What's so special about 5460? Well, nothing in particular, except that as your character threshold crosses it the ContentBounds and Width of the column passes 32767. What's so special about 32767? Other than being the default MaxInputLength of a DataGridViewTextBoxCell, it's the upper limit of a signed short or Int16 (2^15-1). I highly doubt it's a coincidence the issue is occurring here, though not cause of anything to do with MaxInputLength per se. I'd be willing to bet you first noticed the issue at 4563 characters because your font size expanded the width to 32767 as well.

The next question, is why? I'm not really sure. I started following the rabbit hole and disassembled some of the .NET 4.0 DataGridView* libraries to find out. It's a pretty massive and complicated control, and I haven't been able to draw any definite conclusions, but one thing I found that's worth noting is the absolute maximum width a column can assume is 65536, the value of an UNsigned Int16 (2^16):

You see this check in a lot of private internal places when adding or resizing a column, and I tested it. The size won't go larger

This is ironic for two reasons. For one, using the default settings, you can only display 10922 characters (65536 / 6 pixels per character) in a column despite the editing input length being 32767 characters, and programmatically arbitrary.

Second, why would this issue start cropping up at exactly the max of the signed variant of the columns max width? Hmmmm. This is totally a guess, but I think somewhere along the line the max value for whatever renders the text was set as a regular short instead of an unsigned short... or something along those lines. I have my suspicions of the PaintPrivate() method in the implementation of DataGridViewTextBoxCell(), so if you're feeling frisky, maybe put a microscope to it. You'll need an IL disassembler to see this stuff that's not exposed publicly. Specifically, this part of the code I have suspicions of:

  if (text != null && (paint && !flag2 || computeContentBounds))
  {
    int y = cellStyle.WrapMode == DataGridViewTriState.True ? 1 : 2;
    rectangle3.Offset(0, y);
    // ISSUE: explicit reference operation
    // ISSUE: variable of a reference type
    Rectangle& local = @rectangle3;
    // ISSUE: explicit reference operation
    int width = (^local).Width;
    // ISSUE: explicit reference operation
    (^local).Width = width;
    rectangle3.Height -= y + 1;
    if (rectangle3.Width > 0 && rectangle3.Height > 0)
    {
      TextFormatFlags cellStyleAlignment = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(this.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);
      if (paint)
      {
        if (DataGridViewCell.PaintContentForeground(paintParts))
        {
          if ((cellStyleAlignment & TextFormatFlags.SingleLine) != TextFormatFlags.Default)
            cellStyleAlignment |= TextFormatFlags.EndEllipsis;
          TextRenderer.DrawText((IDeviceContext) graphics, text, cellStyle.Font, rectangle3, flag3 ? cellStyle.SelectionForeColor : cellStyle.ForeColor, cellStyleAlignment);
        }
      }
      else
        rectangle1 = DataGridViewUtilities.GetTextBounds(rectangle3, text, cellStyleAlignment, cellStyle);
    }

Sorry for the book!

TL;DR USE A SMALL ASS FONT IF YOU WANT TO PACK CHARACTERS INTO HUGE CELLS.

这篇关于.NET Windows窗体DataGridView当程序添加时,单元文本消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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