c# - How to fit DataGridView width and height to its content? -
i have method create datagridview dynamically when shown up, width greater content width (total width of columns). height has not enough length meet length of rows.
i tried using method didn't work:
datagridview createinputbox(int proc, int mac) { datagridview databox = new datagridview(); (int = 0; < mac; i++) { databox.columns.add("col" + (i + 1), "m" + (i + 1)); databox.columns[i].defaultcellstyle.alignment = datagridviewcontentalignment.middlecenter; databox.columns[i].sortmode = datagridviewcolumnsortmode.notsortable; } databox.rowtemplate.defaultheadercelltype = typeof(customheadercell); databox.rowheaderswidthsizemode = datagridviewrowheaderswidthsizemode.autosizetoallheaders; databox.rowheadersdefaultcellstyle.padding = new padding(2); (int = 0; < proc; i++) { databox.rows.add(); databox.rows[i].headercell.value = "p" + (i + 1); } databox.defaultcellstyle.selectionbackcolor = color.lightgray; databox.allowusertoaddrows = false; databox.allowusertodeleterows = false; databox.allowusertoordercolumns = false; databox.autosizecolumnsmode = datagridviewautosizecolumnsmode.allcells; databox.autosizerowsmode = datagridviewautosizerowsmode.allcells; //this block doesn't work var totalheight = databox.rows.getrowsheight(datagridviewelementstates.none); var totalwidth = databox.columns.getcolumnswidth(datagridviewelementstates.none); databox.width = totalwidth; databox.height = totalheight; // return databox; } public class customheadercell : datagridviewrowheadercell { protected override size getpreferredsize(graphics graphics, datagridviewcellstyle cellstyle, int rowindex, size constraintsize) { var size1 = base.getpreferredsize(graphics, cellstyle, rowindex, constraintsize); var value = string.format("{0}", this.datagridview.rows[rowindex].headercell.formattedvalue); var size2 = textrenderer.measuretext(value, cellstyle.font); var padding = cellstyle.padding; return new size(size2.width + padding.left + padding.right, size1.height); } protected override void paint(graphics graphics, rectangle clipbounds, rectangle cellbounds, int rowindex, datagridviewelementstates cellstate, object value, object formattedvalue, string errortext, datagridviewcellstyle cellstyle, datagridviewadvancedborderstyle advancedborderstyle, datagridviewpaintparts paintparts) { base.paint(graphics, clipbounds, cellbounds, rowindex, cellstate, value, formattedvalue, errortext, cellstyle, advancedborderstyle, datagridviewpaintparts.background); base.paintborder(graphics, clipbounds, cellbounds, cellstyle, advancedborderstyle); textrenderer.drawtext(graphics, string.format("{0}", formattedvalue), cellstyle.font, cellbounds, cellstyle.forecolor); } } result:
as can see width of datagridview control long. how can fit both dimension?
the main problem newly created datagridview has not finished internal layout before being added parent container , still report columns having width = 100.
one way fix call sizing function after dgv has been displayed:
void sizedgv(datagridview dgv) { datagridviewelementstates states = datagridviewelementstates.none; dgv.scrollbars = scrollbars.none; var totalheight = dgv.rows.getrowsheight(states) + dgv.columnheadersheight; totalheight += dgv.rows.count * 4 // correction need var totalwidth = dgv.columns.getcolumnswidth(states) + dgv.rowheaderswidth; dgv.clientsize = new size(totalwidth , totalheight ); } note have fixed few things along way:
- both width , height did not include headers
- changing outer size ignores border. change clientsize instead.
- before size can work need switch off scroll bars.

