简体   繁体   English

DataGridView 上的水平滚动条

[英]Horizontal Scroll Bar on DataGridView

Is there a way to have the horizontal scroll bar while having the AutoSizeMode for the column set to fill?有没有办法让水平滚动条同时让列的 AutoSizeMode 设置为填充?

I have 2 columns, and I want them to fill the width of the DataGridView.我有 2 列,我希望它们填充 DataGridView 的宽度。 When the width of content in the rows exceed the width of the DataGridView, I want to enable the horizontal scroll bar.当行中内容的宽度超过DataGridView的宽度时,我想启用水平滚动条。 Not sure how to do this.不知道该怎么做。 With the research I have done, I found that using the "AllCell" option in the AutoSizeMode would enable the scroll bar, however, I want the rows to fill the DataGridView.通过我所做的研究,我发现在 AutoSizeMode 中使用“AllCell”选项可以启用滚动条,但是,我希望行填充 DataGridView。

"

You can't have both behaviors natively. 您不能同时具有两种行为。
You will have to code it using DataGridViewColumn.MinimumWidth property. 您将必须使用DataGridViewColumn.MinimumWidth属性对其进行编码。
Here is an example using DataGridView 's CellValueChanged event (but you will certainly have to adapt it to your specific case) : 这是一个使用DataGridViewCellValueChanged事件的示例(但您一定要使其适应特定情况):

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    foreach (DataGridViewColumn column in dataGridView1.Columns)
        column.MinimumWidth = column.Width;
    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}

Be careful, this can be very time consuming if you have lots of rows in your datagrid. 请注意,如果您的数据网格中有很多行,这可能会非常耗时。

There's a simple way around it, although it obviously isn't ideal: 有一个简单的解决方法,尽管它显然不理想:

  • add an empty collumn with AutoSizeMode set to Fill at the end 添加一个空的列,将AutoSizeMode设置为结尾
  • and set HeaderText of the last column to an empty string (which I obviousy forgot to do in my demo...) 并将最后一列的HeaderText设置为空字符串(很明显,我在演示中忘了这样做...)

This way it (almost) appears as if the AutoSizeMode of the 2nd column is set to FIll. 这样(几乎)就好像第二列的AutoSizeMode设置为FIll。

Here, Column1 and Column2 have AutoSizeMode set to DisplayedCells, Column3 to Fill: 在这里,Column1和Column2的AutoSizeMode设置为DisplayedCells,Column3设置为Fill:

在此处输入图片说明在此处输入图片说明

This is a really old post, but thought I would add to it.这是一个非常古老的帖子,但我想我会添加它。 If you are not bothered by setting AutoSizeMode to Fill.如果您不介意将 AutoSizeMode 设置为 Fill。

Set the AutoSizeMode of the problematic column to AllCells.将有问题的列的 AutoSizeMode 设置为 AllCells。

This will resize the column header and force it to go out of the visible area thus displaying the horizontal scrollbar.这将调整列标题的大小并强制它离开可见区域,从而显示水平滚动条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM