水平滚动条消失设置最后一列大小以填充
horizontal scrollbar disappear setting the last column size to fill
我有一个包含 4 列的数据网格视图。我要:
- 显示每个单元格内的所有文本(我不想看到任何被“...”截断的文本)
- 最后一列填满剩余的所有内容space
- 水平和垂直滚动条。
使用此代码:
dataGridView.ScrollBars = ScrollBars.Both;
Grid_NonAnatObj.AutoResizeColumns();
GridCol_Visibility.Width = 30;
我看到每个单元格内的所有文本都没有截断,而且我看到了水平和垂直滚动条。
当我尝试添加此代码时
Grid_NonAnatObj.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
为了满足2.,水平滚动条消失。
我该如何解决这个问题?
这是一个 hack-around,但这是我能做的最好的模拟,尽可能接近你想要的结果。
- to display all the texts inside every cell (I don't want to see any texts truncated with "...")
至少,这意味着每列都应将 AutoSizeMode 设置为 DisplayedCells。这将适合您,因此您不必猜测,"Is 30 width enough? Maybe 35 just in case..."。从本质上讲,它还为您的列提供了一种模仿最小宽度的感觉。
但是,如果您的值都很小,并且现在最后一列的右侧有丑陋的未使用区域怎么办?
- that the last column fills all the remaining space
最后一列的条件集 AutoSizeMode to Fill 可以解决这个问题。
- the Horizontal and Vertical scrollbar.
这有点让步,但是当最后一列设置为填充时,您就不需要单杠了。当它设置为 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 < 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);
}
- 显示每个单元格内的所有文本(我不想看到任何被“...”截断的文本)
- 最后一列填满剩余的所有内容space
- 水平和垂直滚动条。
使用此代码:
dataGridView.ScrollBars = ScrollBars.Both;
Grid_NonAnatObj.AutoResizeColumns();
GridCol_Visibility.Width = 30;
我看到每个单元格内的所有文本都没有截断,而且我看到了水平和垂直滚动条。 当我尝试添加此代码时
Grid_NonAnatObj.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
为了满足2.,水平滚动条消失。 我该如何解决这个问题?
这是一个 hack-around,但这是我能做的最好的模拟,尽可能接近你想要的结果。
- to display all the texts inside every cell (I don't want to see any texts truncated with "...")
至少,这意味着每列都应将 AutoSizeMode 设置为 DisplayedCells。这将适合您,因此您不必猜测,"Is 30 width enough? Maybe 35 just in case..."。从本质上讲,它还为您的列提供了一种模仿最小宽度的感觉。
但是,如果您的值都很小,并且现在最后一列的右侧有丑陋的未使用区域怎么办?
- that the last column fills all the remaining space
最后一列的条件集 AutoSizeMode to Fill 可以解决这个问题。
- the Horizontal and Vertical scrollbar.
这有点让步,但是当最后一列设置为填充时,您就不需要单杠了。当它设置为 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 < 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);
}