table problem

Programmers and people with programming knowledge sit here
wazzcob
Posts: 24
Joined: Fri Feb 05, 2021 5:11 am

table problem

Post 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?
User avatar
xizer
Posts: 87
Joined: Mon Oct 05, 2020 9:51 am

Re: table problem

Post 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);
};
wazzcob
Posts: 24
Joined: Fri Feb 05, 2021 5:11 am

Re: table problem

Post 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.
User avatar
xizer
Posts: 87
Joined: Mon Oct 05, 2020 9:51 am

Re: table problem

Post by xizer »

1) Which one?
2) I do not fall on the default settings of the table. What have you customized?
wazzcob
Posts: 24
Joined: Fri Feb 05, 2021 5:11 am

Re: table problem

Post 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;
    }
Post Reply