2

I have a databound datagridview on a form that has a text column that is able to be wrapped. When the columns are refreshed the height of each row is reverted to a single line height even if the content of the row is wrapped (and therefore multiline).

I need to be able to determine programatically if the data in the column for a particular row is currently wrapping or not. Is there a property to check to see if the data is long enough to make it multiline?

Dave
  • 5,436
  • 11
  • 48
  • 74
  • https://stackoverflow.com/questions/17526280/datagridview-to-show-cell-content-completely – Slai Oct 18 '17 at 23:37

2 Answers2

1

I think you're asking a couple different questions; the answer to both is "yes".

First, the length or format of the text. Check the cell's value as a string to see if it contains newlines, or if it is longer than X characters (I leave the exercise of coming up with a good X to you):

if(gridView.Rows[i].Cells[j].Value.ToString().Contains(Environment.NewLine)
      || gridView.Rows[i].Cells[j].Value.ToString().Length > x)
   ...

Second, you can also determine if a row is currently displayed in word-wrap mode; if it is, it should be sized in a combination of horizontal and vertical:

if(gridView.Rows[i].Cells[j].Style.WrapMode = DataGridViewTriState.True)
   ...
KeithS
  • 70,210
  • 21
  • 112
  • 164
  • It doesn't look like the .Value contains any newlines when it's wrapped. – Dave May 06 '11 at 19:58
  • That may well be true; if the actual string doesn't have newlines, then the text will be split based on other word breaks (mostly spaces). Newlines won't be added to the string value just because it's wrapped, much like Notepad doesn't add newlines to the text you've typed in when it's set to word-wrap. However, if a string DOES have newlines, you should usually respect them, which doesn't happen if the WrapMode isn't set to Yes. – KeithS May 06 '11 at 20:29
0

Add the following to your code:

string nl = Environment.NewLine;
dataGridView.Rows.Add(str1 + nl + str2);
dejanualex
  • 3,872
  • 6
  • 22
  • 37