Discussion:
How to get fixed size button in a DataGridViewButtonCell?
(too old to reply)
Mark
2009-10-04 06:05:31 UTC
Permalink
How can I have a fixed size button (just large enough to contain the word
"Delete") in a DataGridViewButton cell? My Delete buttons are filling the
entire cell no matter what the cell size is. One of my columns contains a
variable amount of text and I have WrapMode=true, so the row heights are
different sizes (DataGridView.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders).
John Bundy
2009-10-05 12:59:01 UTC
Permalink
Unless you specify otherwise, the button should always fill the cell. You can
adjust the padding to change this. The question i think is why is your button
column taking up so much space? Change the AutoSizeColumn to
DisplayedCellsExceptHeaders and this should make the buttons (and column) the
size of the text within.
--
-John http://www.jmbundy.blogspot.com/
Please rate when your question is answered to help us and others know what
is helpful.
Post by Mark
How can I have a fixed size button (just large enough to contain the word
"Delete") in a DataGridViewButton cell? My Delete buttons are filling the
entire cell no matter what the cell size is. One of my columns contains a
variable amount of text and I have WrapMode=true, so the row heights are
different sizes (DataGridView.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders).
Mark
2009-10-05 16:46:12 UTC
Permalink
In one column, I'm displaying a variable amount of text with WrapMode=true.
So my row heights will vary and having buttons that are of varying size
doesn't look good. I want a fixed size button (same size for each row)
positioned in the middle of the row.
Post by John Bundy
Unless you specify otherwise, the button should always fill the cell. You can
adjust the padding to change this. The question i think is why is your button
column taking up so much space? Change the AutoSizeColumn to
DisplayedCellsExceptHeaders and this should make the buttons (and column) the
size of the text within.
--
-John http://www.jmbundy.blogspot.com/
Please rate when your question is answered to help us and others know what
is helpful.
Post by Mark
How can I have a fixed size button (just large enough to contain the word
"Delete") in a DataGridViewButton cell? My Delete buttons are filling the
entire cell no matter what the cell size is. One of my columns contains a
variable amount of text and I have WrapMode=true, so the row heights are
different sizes (DataGridView.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders).
Mark
2009-10-05 19:44:57 UTC
Permalink
I set the Padding properties (Left, Right, Top, Bottom) of the
DefaultCellStyle to 3 for the DataGridViewButtonCell. At least now the
button boundary doesn't fill the complete cell and the buttons aren't jammed
on top of each other. It would be nice if there was a property to set the
button height to the same value for each row. Thanks for your help!
Post by John Bundy
Unless you specify otherwise, the button should always fill the cell. You can
adjust the padding to change this. The question i think is why is your button
column taking up so much space? Change the AutoSizeColumn to
DisplayedCellsExceptHeaders and this should make the buttons (and column) the
size of the text within.
--
-John http://www.jmbundy.blogspot.com/
Please rate when your question is answered to help us and others know what
is helpful.
Post by Mark
How can I have a fixed size button (just large enough to contain the word
"Delete") in a DataGridViewButton cell? My Delete buttons are filling the
entire cell no matter what the cell size is. One of my columns contains a
variable amount of text and I have WrapMode=true, so the row heights are
different sizes (DataGridView.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders).
Josh H
2009-12-03 07:02:01 UTC
Permalink
You can set the padding dynamically to keep the button the same size for each
row. If you start with the wrap mode set to false and get the row height
then set the wrap mode to true and get the new row height then you can set
the top and bottom padding to 1/2 the difference in heights and your button
will stay the same size.

Here is a code snippet:

// adjust these for your particular case
int iWrappingCell = 2;
int iButtonCell = 5;

int oldHeight = row.Height;
row.Cells[iWrappingCel].Style.WrapMode =
DataGridViewTriState.True;
int newHeight = row.Height;
int padAmount = (newHeight - oldHeight) / 2);
Padding newPadding = new Padding(0, padAmount, 0, padAmount);
row.Cells[iButtonCell].Style.Padding = newPadding;

If you do this for every row in the grid the buttons will all be the same
height as if the rows were not wrapped. Of course if the row height changes
because the data is modified you will need to reset the padding accordingly.
Loading...