c# - .NET Windows forms DataGridView Cell text disappears when added programatically -


i developing windows forms application includes datagridview. datagridview has 3 columns, of text cells:

  • timestamp
  • connection
  • message

the issue i'm running when add row (programmatically), i'm finding text disappears if long. specific, if text exceeds 4563 characters in length, text disappears.

i know datagridviewtextboxcolumn class has property call maxinputlength can limit number of characters entered. according the documentation, affects text input manually user. i, however, inputting text programmatically.

just make sure though, set property high disappearing text issue still arises when pass 4563 character limit.

one thing have noticed text still there (i.e. scroll bar along bottom can still scrolled though text still there) cannot see text itself. can edit text.

i can add characters until 4563 limit pass that, text disappears. if press backspace return 4563 characters, text reappears.

i developing using .net 4.0, since have support windows xp.

here's short answer disappoint you: it's reported bug , verified microsoft, closed "not important enough fix". there may more instances of it, it's been known since @ least 2011 datagridview control shows blank cell if large string entered , column resized max. "workaround" limit size of cells width, may not satisfactory.

however, curiosity got best of me started looking little deeper; here's first observation worth mentioning:

enter image description here

if @ series of pictures, you'll notice replicated problem default font size/style , specific number 5460. what's special 5460? well, nothing in particular, except character threshold crosses contentbounds , width of column passes 32767. what's special 32767? other being default maxinputlength of datagridviewtextboxcell, it's upper limit of signed short or int16 (2^15-1). highly doubt it's coincidence issue occurring here, though not cause of maxinputlength per se. i'd willing bet first noticed issue @ 4563 characters because font size expanded width 32767 well.

the next question, why? i'm not sure. started following rabbit hole , disassembled of .net 4.0 datagridview* libraries find out. it's pretty massive , complicated control, , haven't been able draw definite conclusions, 1 thing found that's worth noting absolute maximum width column can assume 65536, value of unsigned int16 (2^16):

enter image description here

you see check in lot of private internal places when adding or resizing column, , tested it. size won't go larger

this ironic 2 reasons. one, using default settings, can display 10922 characters (65536 / 6 pixels per character) in column despite editing input length being 32767 characters, , programmatically arbitrary.

second, why issue start cropping @ max of signed variant of columns max width? hmmmm. totally guess, think somewhere along line max value whatever renders text set regular short instead of unsigned short... or along lines. have suspicions of paintprivate() method in implementation of datagridviewtextboxcell(), if you're feeling frisky, maybe put microscope it. you'll need il disassembler see stuff that's not exposed publicly. specifically, part of code 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 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 book!

tl;dr use small ass font if want pack characters huge cells.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -