I have a form in which I use a DataGridView
to present the records from the database.
The point is that the DataGridView
doesn't show me the whole column name and I have to manually enlarge it. That's why I would like the width of the columns to auto-adapt to the size of the text it has .
Here is an image of the Form.
I have tried with:
DataGridView.DefaultCellStyle.Columms.Weith = AutoSizeColumnsMode.AutoZise
But I am getting errors, does anyone have any idea how to make DataGridView column width fit its content?
You can use the DataGridViewColumn.Width property. For example:
More information: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx
Edit: to make it automatic.
To adjust the size of columns and cells, I use the following lines:
Or use the properties of the gridview Greetings
If you use the autofill property, it will create columns of a single size, but if you want any of these columns to be larger I add this property and so this column is larger according to the percentage you gave it.
A quick example would be if you have a datagrid of 100 width and there are 10 columns, all of them will be 10 width each, but if you want column 2 to be larger then you add the value in percentage. in the example it was 200%, that is, twice its size, obviously the value you give it more will be discounted among the rest of the columns.