gpt4 book ai didi

c# - 水平滚动条消失设置最后一列大小以填充

转载 作者:行者123 更新时间:2023-11-30 16:09:16 24 4
gpt4 key购买 nike

enter image description here我有一个包含 4 列的数据 GridView 。我要:

  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. to display all the texts inside every cell (I don't want to see any texts truncated with "...")

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

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

  1. that the last column fills all the remaining space

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

  1. the Horizontal and Vertical scrollbar.

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

请代码:为了通过调整大小保持此行为的一致性,我在 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 < this.dataGridView1.Width)
{
this.dataGridView1.Columns[this.dataGridView1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}

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

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

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

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

关于c# - 水平滚动条消失设置最后一列大小以填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27863788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com