winforms - c# resize datagridview columns to fit control -
i have datagridview docked , anchored panel on winform. when resize form, datagridview resizes expected, columns not resize fit datagridview. instead, left background colour of datagridview.
how can columns grow control?
thanks.
private void datagrid_sizechanged(object sender, eventargs e) { resizegridcolumns(); } private void resizegridcolumns() { //get sum of non-resizable columns width int diffwidth = 0; foreach (datagridviewcolumn col in this.datagrid.columns) { if (col.resizable == datagridviewtristate.false && col.visible) diffwidth += col.width; } //calculate available width int totalresizablewith = this.datagrid.width - diffwidth; //resize column width based on previous proportions this.datagrid.columnwidthchanged -= new datagridviewcolumneventhandler(datagrid_columnwidthchanged); (int = 0; < this.colwidthraport.count; i++) { try { if (this.datagrid.columns[i].resizable != datagridviewtristate.false && this.datagrid.columns[i].visible) { this.datagrid.columns[i].width = (int)math.floor((decimal)totalresizablewith / this.colwidthraport[i]); } } catch { } } this.datagrid.columnwidthchanged += new datagridviewcolumneventhandler(datagrid_columnwidthchanged); } private void datagrid_columnwidthchanged(object sender, datagridviewcolumneventargs e) { calculategridcolwidthsraport(); } /// <summary>calculates proportions between grid width , column width</summary> private void calculategridcolwidthsraport() { //get sum of non-resizable columns width int diffwidth = 0; int colwidthssum = 0; foreach (datagridviewcolumn col in this.datagrid.columns) { if (col.visible) { colwidthssum += col.width; if (col.resizable == datagridviewtristate.false) diffwidth += col.width; } } colwidthssum += 24; //calculate available int totalresizablewith = colwidthssum - diffwidth;// this.datagrid.width - diffwidth; if (this.parentform.windowstate == formwindowstate.maximized) { totalresizablewith = this.datagrid.width - diffwidth; } //calculate proportions of each column relative available width this.colwidthraport = new list<decimal>(); foreach (datagridviewcolumn col in this.datagrid.columns) { this.colwidthraport.add((decimal)totalresizablewith / (decimal)col.width); } }
Comments
Post a Comment