DatagridView Button Column Cell Editing Control Width.
I have a datagridview with a button column as one of its column collection. The width of the button column is say 50 but the problem is the button editing control within the cell of that column is also having the same width as its parent column which looks awkward. I want to set the width of the button editing control small enough say 20 irrespective of its parent column width. Please help. Thanks.
Shaik AliPosted Feb 19, 2010, 2:26 AM
You have two ways to achieve.
1. Creating user control with textbox and button(with size you mentioned). Attach the same to the column u wish.
2. You can also achieve the same using button control for the column. i have achieved the size of the button with little modifications to Sams code. I have taken client rectangle and taken the x position from that. Find the code snippet of my code
Rectangle rect = dataGridView1.GetCellDisplayRectangle(Column_5.Index, 0, true);
Padding p = Column_5.DefaultCellStyle.Padding;
p.Left = rect.X + 75; //my column width is 100 adjusting x to 75 so that button goes to corner of the cell
p.Right = rect.Y;
Column_5.DefaultCellStyle.Padding = p;
If my reply helped you, then please check the "Do you like this Answer" checkbox
ho vangPosted Oct 5, 2010, 3:42 AM
I want display only button in dgv.?
Sam HobbsPosted Feb 28, 2010, 1:53 PM
PriyamtheonePosted Feb 28, 2010, 9:44 AM
Sam HobbsPosted Feb 4, 2010, 1:41 PM
Sam HobbsPosted Nov 13, 2009, 3:21 PM
You can set the padding, as in:
That will set the width of the cell contents (the button) to 40. In the code above, I set the margin to half of the column width minus 40, unless the column width is less than 40.
Sam HobbsPosted Nov 13, 2009, 2:49 PM
I don't know if it is possible to modify the button's width more directly as you are trying to do; it seems that .Net is not designed to allow it. You can make other changes, such as the baclground color of the button, using:
Where Column2 is the name of the column with the button. The CellTemplate size however is read-only.