Page 1 of 2
table problem
Posted: Mon Jun 07, 2021 5:11 pm
by wazzcob
There is a table (DataGridView), it has 3 columns - the first two are of fixed size, the last one has AutoSizeMode = Fill.
Everything is buzzing, but if the text in the last column does not fit when the shape is reduced, it is cut off and "..." is added.
I also need a skollbar to appear instead of cutting off the text, allowing you to scroll through the contents of the table.
Shaw would you advise?
Re: table problem
Posted: Tue Jun 08, 2021 7:34 am
by xizer
Well, for example, remove AutoSizeMode = Fill and count the size of the last column by hand. If it is smaller than the area allotted for the table, stretch it, if it is larger, leave the PreferredSize:
Code: Select all
int initGridSize = grid.Width - grid.Columns[grid.ColumnCount - 1].Width;
grid.Resize += (sender, e) =>
{
int lastColumnSize = (from DataGridViewRow row in grid.Rows select row.Cells[row.Cells.Count - 1].PreferredSize.Width).Max();
grid.Columns[grid.ColumnCount - 1].Width = Math.Max(lastColumnSize, grid.Width - initGridSize);
};
Re: table problem
Posted: Wed Jun 09, 2021 6:10 am
by wazzcob
xizer wrote: ↑Tue Jun 08, 2021 7:34 am
Well, for example, remove AutoSizeMode = Fill and count the size of the last column by hand. If it is smaller than the area allotted for the table, stretch it, if it is larger, leave the PreferredSize:
Code: Select all
int initGridSize = grid.Width - grid.Columns[grid.ColumnCount - 1].Width;
grid.Resize += (sender, e) =>
{
int lastColumnSize = (from DataGridViewRow row in grid.Rows select row.Cells[row.Cells.Count - 1].PreferredSize.Width).Max();
grid.Columns[grid.ColumnCount - 1].Width = Math.Max(lastColumnSize, grid.Width - initGridSize);
};
thanks. but I get an exception when I try to change the column width in the Resize event.
Re: table problem
Posted: Wed Jun 09, 2021 9:13 am
by xizer
1) Which one?
2) I do not fall on the default settings of the table. What have you customized?
Re: table problem
Posted: Wed Jun 09, 2021 7:05 pm
by wazzcob
Unlucky again, I managed to catch an exception again.
How I proceeded:
1. Set AutoResizeMode = Fill.
2. In the event handler ColumnWidthChanged tried to change the width of the column:
Code: Select all
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
int w = e.Column.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, true);
if (e.Column.Width < w)
e.Column.Width = w;
}