c# - get actual column width -
i building datagridview table programmatically , placing within groupbox on form.
i insert data, resize columns automatically fit data. want resize groupbox size of datagridview. each column , row, respective width , height, technically should accurate, , update panel , overall datagridview sizes.
the problem column.width returns 100 pixels regardless of actual size (see screenshot: actual column width around 30px, not 100). if manually enter width = 90 pixels, resizing quite accurate!
matrix = new datagridview(); //modify behaviour matrix.columnheadersvisible = false; matrix.allowusertoresizecolumns = false; matrix.autosizecolumnsmode = datagridviewautosizecolumnsmode.allcells; matrix.rowheadersvisible = false; matrix.allowusertoresizerows = false; //modify positioning matrix.location = new point(10, 20); //matrix.anchor = (anchorstyles)(anchorstyles.left | anchorstyles.top | anchorstyles.bottom | anchorstyles.right); matrix.dock = dockstyle.fill; //set size of matrix matrix.columncount = col; matrix.rowcount = row; //data inserted... matrix.autoresizecolumns(); //correctly resizes columns int height = 0; foreach (datagridviewrow row in matrix.rows) { height += row.height; } height += matrix.columnheadersheight; int width = 0; foreach (datagridviewcolumn col in matrix.columns) { width += col.width; //problem: width = 100 pixels. } width += matrix.rowheaderswidth; //width = 90; //override width manually matrix.size = new size(width + 2, height + 2); panel.size = new size(matrix.width, matrix.height);
the panel large because width isn't 90px 357 roughly, wrong!
edit: partial fix have found way correct width of cell:
datagridview.rows[0].cells[0].contentbounds.width //contentbounds = rectangle exact dimensions of cell
i can set datagridview correct size, if it's not docked fill. setting matrix.dock = dockstyle.fill prevents resizing occur correctly.
i guess because you're autosizing columns in row:
matrix.autosizecolumnsmode = datagridviewautosizecolumnsmode.allcells;
try change to:
matrix.autosizecolumnsmode = datagridviewautosizecolumnsmode.none;
Comments
Post a Comment