Reputation: 9736
UI:
Btn click code:
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column 1");
dt.Columns.Add("Column 2");
dt.Columns.Add("Column 3");
dt.Columns.Add("Column 4");
dt.Columns.Add("Column 5");
DataRow dr = dt.NewRow();
dr["Column 1"] = "Col1 Test";
dr["Column 2"] = "Col2 Test";
dr["Column 3"] = "Col3 Test";
dr["Column 4"] = "Col4 Test";
dr["Column 5"] = "Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test Col5 Test ";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
dataGridView1.Columns["Column 5"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
dataGridView1.Columns["Column 5"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
Result:
If I reduce the size of gridview, via the visual studio designer and relaunch the app, then the horizontal bar doesn't show up.
UI:
Output:
How to make datagridview row and column wrap and autosize such that it doesn't cramp up in the space? I want it to dynamically adjust the row height and automatically show the horizontal scrollbar.
Upvotes: 0
Views: 1303
Reputation: 9479
I am not sure exactly what you are wanting to achieve or HOW the grid is sized to begin with. Is the user able to “resize” the grid or columns? Is the goal to set the grid’s width (up to a certain size) to show all the columns? In other words, what is the overall goal in relation to the grid and the columns in it.
In a short answer… The issue you are having is because the DataGridView
is just not very smart. In addition to add confusion to the picture, but does not apply here, is that there are two (2) AutoSizeColumnMode
settings… let me try to explain…
For starters, there are two (2) different “AutoSizeMode” properties that can be used to auto size columns in a grid and this may cause some confusion. One is for the WHOLE GRID… the AutoSizeColumnMode
which applies to the grid… and there is another property for the individual columns… AutoSizeMode
.
I am confident that whatever the GRID’S AutoSizeColumnMode
is set to will OVERRIDE any individual column setting if there is a conflict. Example IF the GRID’S AutoSizeColumnMode
is set to Fill
… THEN, IF… you set an individual column’s AutoSizeMode
to something other than Fill
… THEN… the GRID’S Fill
setting will be used.
In most cases I have found that setting the GRIDS auto size mode to NONE
and manually set the individual columns auto size mode is at least predictable and consistent.
Another issue is that the grid is not very smart and will obligingly stuff 100’s of columns of data into a grid that is not wide enough to hold all the columns. The minimum size of each column would be set to something like 5 pixels. Obviously this is not useful since you cannot see the data, but the grid will obligingly do this this if the setting is set to “Fill” and there are too many columns and/or the grid's width is not wide enough. This is similar to what you see in the last picture with column 5.
In this particular situation with your posted code…
The reason the last picture does NOT show a horizontal scroll bar is because the last column’s (Column 5) AutoSizeMode
is set to Fill
… AND… the GRIDS WIDTH is wide enough such that, column 5 still has room to “FILL” the grids width. This is why you see the vertical scroll bar but no horizontal scroll bar, since the column still “FITS” in the grid…
IF… you make the GRIDS WIDTH less than the width of the first four (4) columns, THEN, the horizontal scroll bar will display, HOWEVER, if you scroll to the right to see column 5…. column 5 will have a width of something like 5 pixels which obviously is of no use since we would not be able to see any of the data.
I hope the above info helps and given what you describe I am guessing what you are looking for is to NOT set columns 5’s AutoSizeMode
to Fill
… is what you want is to set column 5’s AutoSizeMode
to AllCells
.
dataGridView1.Columns["Column 5"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
This will produce something like shown below.
Bear in mind, that IF the grid is wide enough and column 5’s width is such that it leaves a gap at the end, THEN you will need to look for this in your code. Example you would know that if the grids width is greater than some value, then yes, you want to set column 5’s auto size mode to Fill
, if it is less than a certain value then you want to set it to AllCells
.
Unfortunately, as I previously commented, the grid is not very smart and if you want the columns to display in a particular manner, then it may be necessary create a method to examine the columns and determine what setting to give each column. I have done this in the past and it is not that difficult to get around the grid’s shortcomings.
Upvotes: 1