瑞峰的博客

导航

C# winform 里DataGrid 如何滚动到某行

DataGrid里提供了一个保护的方法,必须继承一个DataGrid,然后公开这个方法就行了,方法如下:
(想同时移动DataGrid左边的小三角,只需在绑定完后,执行如下代码:
BindingManagerBase myBindingManagerBaseParent = this.BindingContext[DataGrid1.DataSource];
myBindingManagerBaseParent.Position = CurrentRow;
)

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace MyWinForm
{
 /// <summary>
 /// UserControl1 的摘要说明。
 /// </summary>
 public class MyDataGrid : System.Windows.Forms.DataGrid
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public MyDataGrid()
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent();

   // TODO: 在 InitComponent 调用后添加任何初始化

  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if( components != null )
     components.Dispose();
   }
   base.Dispose( disposing );
  }

  public void ScollRow(int row)
  {
   if (row >0)
   {
    base.GridVScrolled(this,new ScrollEventArgs(ScrollEventType.LargeIncrement, row));
   }
  }

  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   components = new System.ComponentModel.Container();
  }
  #endregion
 }
}


posted on 2006-08-04 15:44  峰子  阅读(1464)  评论(4)    收藏  举报