views:

139

answers:

2

I'm using an editable DataGridView Windows form to display a list of variables which can have different types of information, the format of which is unknown to the application: paths, strings, numbers, and so on. The form is using source data binding.

Some of the values may be long, and I would like to display them on several lines rather than a long, partially visible line. The original WrapMode is not appropriate in my case because it splits the lines on space characters, which are not always present: typical examples are a long PATH variable made up of semi-colon-separated paths, or hex digits.

Wrapping lines at the form border rather than on specific characters would be fine, though only doing that if necessary would be better. Adding other split positions, instead of space characters, only would be even better.

Performance-wise: the grid row count will never be very high: usually 0 to 10, could go as high as, say 50, in some extreme cases, and normally the maximum length of each shouldn't be more than 4 or 5 lines when wrapped. So it's not a big concern.

How would you recommend to do that?

  • handling the CellFormatting event, by inserting \r characters and removing them when parsing the committed value? The problem with this is that the cell seems to have its own way of re-formatting the text in input mode, changing its aspect when switching between the two modes. Possibly other side-effects yet to be discovered...
  • or is there a way to modify the WrapMode behaviour that I overlooked?
A: 

I do the same thing but without binding by setting the following:

this.dataGridView1.Rows[this.RowIndex].Height = 100;
var style = row.Cells[this.ColumnIndex].Style;
style.WrapMode = DataGridViewTriState.True;
style.Alignment = DataGridViewContentAlignment.TopLeft;

Perhaps setting these members on the DefaultCellStyle property of the column before you DataBind() will do the same for you.

Codesleuth
I'm already using the WrapMode by setting it to this value, the problem is that it relies on space characters to find where to split the lines, and there are not always such characters in my case, so it doesn't split the lines and I end up with a horizontal scroll bar.
RedGlyph
+2  A: 

You could always insert the spaces in run-on strings, I dont know of any other options when your using a gridview, the formatting is a little limited there.

You could try something like this if you really need to display it in a fixed width cell:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string cellTxt = e.Row.Cells[1].Text;
    int lengthBeforeBreak = 25;
    if (e.Row.RowType == DataControlRowType.DataRow && cellTxt.Length > lengthBeforeBreak)
    {
        int curPos = 0;
        for (int i = 0; i < Decimal.Round((cellTxt.Length/lengthBeforeBreak),0); i++)
   {
            if (cellTxt.IndexOf(" ", curPos, lengthBeforeBreak) == -1)
            {
                cellTxt = cellTxt.Insert(curPos+lengthBeforeBreak, " ");
            }
            curPos += lengthBeforeBreak + 1;
   }
        e.Row.Cells[1].Text = cellTxt;
    }

}
Tj Kellie
I thought about inserting \r instead of spaces, because the program needs to know which characters to remove when validating the input (can't let those space characters in the string), as mentioned in the question... That's the only option I currently have. But then I'd have to calculate exactly where to insert them, using the number of pixels each part of the string takes and comparing them to the cell width, something I'm not really looking forward to. I was hoping to re-use what's already in the framework... Thanks anyway :)
RedGlyph