DataGridView - how to toggle multiline on a row?

  • Thread starter Richard Lewis Haggard
  • Start date
R

Richard Lewis Haggard

I have a DataGridView which has a cell that is going to contain what might
be a large amount of text data. The UI designed has decreed that each row
will have a button that toggles the row's multiline display such that, when
the button is up, the row is a single line and when the button is down, the
row enters a multiline mode so that the cell can expand to a height great
enough to display all of the cell's contents. I've figured out how to make a
column of buttons but the toggling between multi and single line is giving
me trouble. How can this be done?
 
C

ClayB

Here is one way you can attempt to do this. Handle the CellPainting
event. In your event handler, somehow test whether the cell being
painted is one of your potential multiline cells (not sure how you
would do this in your particular case, but you would have the row and
column index among other properties that you could use in your test).
If the cell should be multiline, then set the WrapMode property. Then,
finally, in your button click code, you would some how make the grid
refresh itself (maybe calling dataGridView.Refresh) so the cell would
be redrawn with the correct WrapMode setting. This way every time teh
cell is drawn, the proper WrapMode is dynamically determine.


void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2 && e.RowIndex > -1 &&
dataGridView1[0, e.RowIndex].Value.Equals("1"))
{
e.CellStyle.WrapMode = DataGridViewTriState.True;
}
}

===================
Clay Burch
Syncfusion, Inc.
 
Q

Quoc Linh

Here is one way you can attempt to do this. Handle the CellPainting
event. In your event handler, somehow test whether the cell being
painted is one of your potential multiline cells (not sure how you
would do this in your particular case, but you would have the row and
column index among other properties that you could use in your test).
If the cell should be multiline, then set the WrapMode property. Then,
finally, in your button click code, you would some how make the grid
refresh itself (maybe calling dataGridView.Refresh) so the cell would
be redrawn with the correct WrapMode setting. This way every time teh
cell is drawn, the proper WrapMode is dynamically determine.

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2 && e.RowIndex > -1 &&
dataGridView1[0, e.RowIndex].Value.Equals("1"))
{
e.CellStyle.WrapMode = DataGridViewTriState.True;
}
}

===================
Clay Burch
Syncfusion, Inc.

I usually display multi-line text in a textbox within the cell, and
show/hide the textbox accordingly.
Another approach maybe using tooltip to display the full text if mouse
hover-over. There are
some custom tooltip out there.

Quoc Linh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top