【问题标题】:DatagridView Horizontal scroll to scroll off exact width of columns, should not cut off from the midDatagridView 水平滚动以滚动列的确切宽度,不应从中间切断
【发布时间】:2013-11-27 17:12:30
【问题描述】:

我有一个 DataGridview。它有一个 dgv_scroll 事件。在水平滚动中(在移动滚动条时),我需要它应该完全冲掉列的宽度。就像我们单击 Datagridview 滚动条的末端箭头(都在右端和左端)时一样。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    在DataGridView滚动事件中添加这个

    if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
    {
        e.NewValue = GetColumnOffset(e.NewValue); 
    }
    

    然后定义方法

    private int GetColumnOffset(int offset)
    {
        int start = 0, end = 0;
        DataGridViewColumnCollection Columns = dgvBudgetPeriods.Columns;
        foreach (var column in Columns.Cast<DataGridViewColumn>().Where(c => !c.Frozen))
        {
            end = start + column.Width;
            if (start <= offset && offset < end)
            {
                break;
            }
            start = end;
        }
        return start == offset ? offset : end;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-27
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多