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

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

问题描述

我已经有4列一个DataGridView。我想:




  1. 来显示每一个细胞里面所有的文本(我不希望看到任何截断文本... )

  2. 最后一列填充所有剩余空间

  3. 的水平和垂直滚动条。



使用此代码:

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



我看没有截断每一个细胞里面的所有文字,我看到的水平和垂直滚动条两种。
当我尝试添加此代码



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



为了满足2,水平滚动条消失。
我怎样才能解决这个问题?


解决方案

这是一个黑客绕,但是这是我能做到的最好嘲笑尽可能接近你想要的结果。





  1. 来显示每一个细胞里面所有的文本(我不不想看到任何文本截断用...)




目前最起码,这意味着每个列应该有AutoSizeMode设置为DisplayedCells。这将做拟合你,让你不必猜测,是30宽就够了吗?也许35以防万一......。本质上,它也给你列一个模仿最小宽度的感觉。



但是,如果你的价值观都是小,现在你有一个对的右侧丑未使用的区域最后一列?





  • 最后一列填充所有剩余空间




  • 一个条件集中的最后一个栏AutoSizeMode来填充可以解决这个问题的。





  • 的水平和垂直滚动条。




  • 这是一个有点给予和索取,当最后一列设置为补,你就没有必要了单杠。当它被设置为DisplayedCells,列要么正好适合你的宽度或它们比你的宽度大,在这种情况下栏将显示



    CODEZ PLZ:
    。要保持这种行为通过调整大小一致,我实现它在DGV resize事件。

     私人无效dataGridView1_Resize(对象发件人,EventArgs E)
    {
    INT宽度= this.dataGridView1.RowHeadersWidth;

    的foreach(在this.dataGridView1.Columns的DataGridViewColumn列)
    {
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    宽度+ = col.Width;
    }

    如果(宽度LT; this.dataGridView1.Width)
    {
    this.dataGridView1.Columns [this.dataGridView1.Columns.Count - 1]。 AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    }
    }



    问题:这个工程很大,但也需要触发形式构造从一开始就正确显示。

     公共Form1中()
    {
    本.InitializeComponent();

    this.Examples =新的BindingList<例子>()
    {
    新实例(){首先=富,最后=酒吧,测试=小 }
    新实例(){首先=我,最后=示例,测试=您。 }
    };

    this.dataGridView1.DataSource = this.Examples;

    THIS.VISIBLE = TRUE; //做这做初步调整大小时,列仍然是100的宽度。
    this.dataGridView1_Resize(this.dataGridView1,EventArgs.Empty); //调用我们的变化。
    //this.Examples[0].Test =ReallyBigExampleOfTextForMySmallLittleColumnToDisplayButResizeToTheRescue;
    }



    编辑:如果您的电池是可编辑的,有一个机会,长期的数据可以输入造成可怕的省略号......

     私人无效dataGridView1_CellEndEdit(对象发件人,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天全站免登陆