3
votes

enter image description hereI've a datagridview having 4 columns. I want:

  1. to display all the texts inside every cell (I don't want to see any texts truncated with "...")
  2. that the last column fills all the remaining space
  3. the Horizontal and Vertical scrollbar.

Using this code:

  dataGridView.ScrollBars = ScrollBars.Both;
  Grid_NonAnatObj.AutoResizeColumns();
  GridCol_Visibility.Width = 30;

I see all the texts inside every cell without truncations and I see both the horizontal and vertical scrollbar. When I try to add this code

  Grid_NonAnatObj.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

in order to satisfy 2., the horizontal scrollbar disappear. How can I solve this problem?

1
What properties are you setting on the other colums? - Abhishek
I doubt this is possible nor do I see any sense in doing it as it never has anything to scoll.. Unless you want to show it for its own sake.. (in that case maybe add a HScrollbar control to the DGV.Controls and Dock it to the Bottom.) - TaW
@Abhishek: I've not set other colums properties, I've added a pic about the properties of my datagridview - Martina
@TaW: I'm using Visual Studio 2008, that does not have HScrollbar control - Martina
From MSDN documentation on DataGridViewAutoSizeColumnMode.Fill: "The column width adjusts so that the widths of all columns exactly fills the display area of the control, requiring horizontal scrolling only to keep column widths above the DataGridViewColumn.MinimumWidth property values." This leads me to believe like @TaW said, the scrollbars shouldn't show since the grid horizontally fits exactly. The exception case might be if the column minwidths push the columns past the grid width. However, I don't how that would work, which is why this is merely a comment. - OhBeWise

1 Answers

3
votes

It's a hack-around, but this was the best I could do to mock as closely the results you wanted.

  1. to display all the texts inside every cell (I don't want to see any texts truncated with "...")

At the very minimum, this means each column should have AutoSizeMode set to DisplayedCells. This will do the fitting for you so you don't have to guess, "Is 30 width enough? Maybe 35 just in case...". Essentially it also gives your columns a mimicked minimum width feel.

But what if your values are all small and now you have that ugly unused area on the right-hand side of the last column?

  1. that the last column fills all the remaining space

A conditional set of the last columns AutoSizeMode to Fill can fix this.

  1. the Horizontal and Vertical scrollbar.

It's a little give-and-take, but when the last column is set to fill, you'll have no need of the horizontal bar. When it's set to DisplayedCells, the columns either exactly fit your width or they are larger than your width, in which case the bar will show.

CODEZ PLZ: To keep this behavior consistent through resizes, I implemented it in the dgv Resize event.

private void dataGridView1_Resize(object sender, EventArgs e)
{
  int width = this.dataGridView1.RowHeadersWidth;

  foreach (DataGridViewColumn col in this.dataGridView1.Columns)
  {
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    width += col.Width;
  }

  if (width < this.dataGridView1.Width)
  {
    this.dataGridView1.Columns[this.dataGridView1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  }
}

Problem: This works great, but also needs to be triggered in the form constructor to display correctly from the start.

public Form1()
{
  this.InitializeComponent();

  this.Examples = new BindingList<Example>() 
  {
    new Example() { First = "Foo", Last = "Bar", Test = "Small" },
    new Example() { First = "My", Last = "Example", Test = "You." }
  };

  this.dataGridView1.DataSource = this.Examples;

  this.Visible = true; // Do this or during the initial resize, the columns will still be 100 width.
  this.dataGridView1_Resize(this.dataGridView1, EventArgs.Empty); // Invoke our changes.
  //this.Examples[0].Test = "ReallyBigExampleOfTextForMySmallLittleColumnToDisplayButResizeToTheRescue";
}

Edit: If your cells are editable and there's a chance long data may be entered causing the dreaded ellipsis...

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
  this.dataGridView1_Resize(this.dataGridView1, EventArgs.Empty);
}