2011-11-21 5 views
6

우리는 모두 마우스 버튼을 누른 상태에서 마우스를 그리드 가장자리로 이동하고 열/행을 스크롤하고 선택 항목이 커지는 기능에 익숙합니다.마우스로 DataGridView 스크롤

성능 문제로 인해 MultiSelect를 해제하고 선택 프로세스를 직접 처리해야하는 DataGridView 기반 컨트롤이 있고 이제는 클릭 + 홀드 스크롤링 기능도 사용할 수 없게됩니다.

이 기능을 다시 쓰는 방법에 대한 제안 사항이 있으십니까?

나는 MouseLeave 이벤트와 같은 간단한 것을 사용하려고 생각했지만 동적 스크롤 속도를 구현하는 것뿐만 아니라 어떤 위치를 남겼는지를 결정하는 방법을 모르겠습니다.

+0

질문에 좀 더 구체적으로 설명해 주실 수 있습니까? 코드 조각을 넣을 수 있습니까? – Priyank

+0

아직 아무 것도하지 않았습니다 ... 나는 코딩하기 전에 이것을 접근하기 위해 합리적인 방법으로 몇 가지 (일반적인) 지침을 얻기를 희망했습니다. – ChandlerPelhams

답변

7

은 그냥 를 Form1_Load

DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel); 

에이 코드를 추가하고이 중 하나는 System.ArgumentOutOfRangeException이 발생하지 않습니다 마우스 휠 이벤트

void DataGridView1_MouseWheel(object sender, MouseEventArgs e) 
{ 
    int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex; 
    int scrollLines = SystemInformation.MouseWheelScrollLines; 

    if (e.Delta > 0) 
    { 
     this.DataGridView1.FirstDisplayedScrollingRowIndex 
      = Math.Max(0, currentIndex - scrollLines); 
    } 
    else if (e.Delta < 0) 
    { 
     this.DataGridView1.FirstDisplayedScrollingRowIndex 
      = currentIndex + scrollLines; 
    } 
} 
+0

가끔은이 System.ArgumentOutOfRangeException있어 – Timeless

1

입니다 경우 :

void DataGridView1_MouseWheel(object sender, MouseEventArgs e) 
{ 
    int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex; 
    int scrollLines = SystemInformation.MouseWheelScrollLines; 

    if (e.Delta > 0) 
    { 
     this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines); 
    } 
    else if (e.Delta < 0) 
    { 
     if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines)) 
      this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines; 
    } 
} 
2

답장 완료 Focus Datagridview를 설정해야합니다

private void DataGridView1_MouseEnter(object sender, EventArgs e) 
     { 
      DataGridView1.Focus(); 
     } 

then Add Mouse wheel event in Load function 
DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel); 

Finally Create Mouse wheel function 

void DataGridView1_MouseWheel(object sender, MouseEventArgs e) 
{ 
    int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex; 
    int scrollLines = SystemInformation.MouseWheelScrollLines; 

    if (e.Delta > 0) 
    { 
     this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines); 
    } 
    else if (e.Delta < 0) 
    { 
     if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines)) 
      this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines; 
    } 
} 

잘 작동합니다.

관련 문제