似若流云
QQ:78009151

我们都知道Winfrom中,在正常情况下,左右键并不能直接控制DataGridView的水平滚动条滚动,它只能控制选中单元的移动,但在有些情况下,我们需要通过左右键来实现一列一列的滚动,那么我们可以这样做:

在ProcessCmdKey的重载方法中写:

 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            
if (keyData == Keys.Left)
            {
                
if (this.HorizontalScrollingOffset > 0)
                {
                    
if (this.FirstDisplayedScrollingColumnHiddenWidth != 0)
                    {
                        
this.HorizontalScrollingOffset -= this.FirstDisplayedScrollingColumnHiddenWidth;
                    }
                    
else
                    {
                        
this.HorizontalScrollingOffset -= 2;
                        
this.HorizontalScrollingOffset -= this.FirstDisplayedScrollingColumnHiddenWidth;
                    }
                }
                return true;
            }
            
else if (keyData == Keys.Right)
            {
                
if (this.HorizontalScrollingOffset < this.HorizontalScrollBar.Maximum)
                {
                    
this.HorizontalScrollingOffset += this.Columns[this.FirstDisplayedScrollingColumnIndex].Width - this.FirstDisplayedScrollingColumnHiddenWidth;
                }
                return true;
            }


            
return base.ProcessCmdKey(ref msg, keyData);
        }
posted on 2008-11-26 10:13  myjece  阅读(3616)  评论(3编辑  收藏  举报