水平滚动条消失设置要填充的最后一列大小 [英] horizontal scrollbar disappear setting the last column size to fill

查看:17
本文介绍了水平滚动条消失设置要填充的最后一列大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有 4 列的数据网格视图.我要:

  1. 显示每个单元格内的所有文本(我不想看到任何被..."截断的文本)
  2. 最后一列填满所有剩余空间
  3. 水平和垂直滚动条.

使用此代码:

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

我看到了每个单元格内的所有文本而没有被截断,并且我看到了水平和垂直滚动条.当我尝试添加此代码时

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

为了满足2.,水平滚动条消失.我该如何解决这个问题?

解决方案

这是一个 hack-around,但这是我所能做的最好的模拟你想要的结果.

<块引用>

  1. 显示每个单元格内的所有文本(我不想看到任何被..."截断的文本)

至少,这意味着每列都应将 AutoSizeMode 设置为 DisplayedCells.这将适合您,因此您不必猜测,30 的宽度是否足够?也许 35 以防万一......".从本质上讲,它还为您的列提供了一种模仿最小宽度的感觉.

但是,如果您的值都很小,现在最后一列的右侧有丑陋的未使用区域怎么办?

<块引用>

  1. 最后一列填满所有剩余空间

最后一列 AutoSizeMode to Fill 的条件集可以解决这个问题.

<块引用>

  1. 水平和垂直滚动条.

这有点让步,但是当最后一列设置为填充时,您将不需要单杠.当它设置为 DisplayedCells 时,列要么完全适合您的宽度,要么大于您的宽度,在这种情况下,将显示栏.

CODEZ PLZ:为了通过调整大小保持这种行为的一致性,我在 dgv Resize 事件中实现了它.

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 

问题:这很好用,但还需要在表单构造函数中触发才能从一开始就正确显示.

public Form1(){this.InitializeComponent();this.Examples = new BindingList(){new Example() { First = "Foo", Last = "Bar", Test = "Small" },new Example() { First = "My", Last = "Example", Test = "You."}};this.dataGridView1.DataSource = this.Examples;this.Visible = true;//执行此操作或在初始调整大小期间,列的宽度仍为 100.this.dataGridView1_Resize(this.dataGridView1, EventArgs.Empty);//调用我们的更改.//this.Examples[0].Test = "ReallyBigExampleOfTextForMySmallLittleColumnToDisplayButResizeToTheRescue";}

如果您的单元格是可编辑的并且有可能输入长数据导致可怕的省略号...

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

I'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?

解决方案

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);
}

这篇关于水平滚动条消失设置要填充的最后一列大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆