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 Ali99 reputationPosted Feb 18, 2010, 9:26 PM EST
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
Sam HobbsRank #107 · 20.5K reputationPosted Nov 13, 2009, 9:49 AM EST
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.
Sam HobbsRank #107 · 20.5K reputationPosted Nov 13, 2009, 10:21 AM EST
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 HobbsRank #107 · 20.5K reputationPosted Feb 4, 2010, 8:41 AM EST
Priyamtheone59 reputationPosted Feb 28, 2010, 4:44 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Feb 28, 2010, 8:53 AM EST
ho vang6 reputationPosted Oct 4, 2010, 11:42 PM EDT
I want display only button in dgv.?