2013-07-22 3 views
-1

양식에 2 개의 격자가 있습니다. 하나의 표가 일부 학생 정보로 채워집니다. 그리고 나는 mouse를 dataGridview1 위에 놓았을 때 Popup 같은 다른 gridview를 보여 주거나 다른 DataGridview에 Name 열을 기반으로 한 다른 DataGridview를 채우기를 원합니다.마우스를 가져 가면서 열의 값을 가져 오는 방법

그리드가 팝업으로 표시되는 부분을 마우스로 따라갔습니다. 이 그리드 잎 또한 때 사라집니다 :

private void dataGridView1_MouseHover(object sender, EventArgs e) 
{ 
    SqlDataAdapter da2; 
    DataTable dt2 = new DataTable(); 
    da2 = new SqlDataAdapter("SELECT ID, Name, Surname, City"+ 
          "FROM tblStudents2" + 
          "WHERE Name = **what to write here**", con); 

    da2.Fill(dt2); 
    dataGridView2.DataSource = dt2; 
} 

private void dataGridView1_MouseLeave(object sender, EventArgs e) 
{ 
    dataGridView2.Visible = false; 
} 

private void dataGridView1_MouseMove(object sender, MouseEventArgs e) 
{ 
    dataGridView2.Visible = true; 
    dataGridView2.Location = new Point(MousePosition.X-100, MousePosition.Y-100); 
} 

을 그냥 부분에서, 나는에게 SQL 문을 어떻게 작성하는 당신을 물어보고 싶은 : 이름 = '_ __ _' ??

Mous가 첫 번째 행 위에있을 때 Name (열 인덱스 1)을 가져 와서 다른 DataGrid를 채우기를 원합니다. 나는 희망

당신이 날 :(이해

***은 그렇지 않습니다 * ... 여기 내 코드는 에산의 코드를 거쳐입니다 편집이 거의. 문제가 workes 다음 행으로 마우스를 이동 한 후 그리드를 다시 채 웁니다 !! Butif DataGrid를두고 다른 행에 마우스를 올려 놓으면이 행의 ifnormation이 표시됩니다. 마우스를 움직여 새로 고침 그리드를 만드는 방법 다음 행?

public void LoadGridi2() 
    { 
     SqlDataAdapter da2; 
     DataTable dt2 = new DataTable(); 
     da2 = new SqlDataAdapter("SELECT ID, Name, Surname, City FROM tblMentori WHERE Name = '" + dataGridView1.Rows[row.Index].Cells[1].Value.ToString() + "'", con); 

     da2.Fill(dt2); 

     dataGridView2.DataSource = dt2; 
    } 
    private void dataGridView1_MouseHover(object sender, EventArgs e) 
    { 
     LoadGridi2(); 
    } 

    private void dataGridView1_MouseLeave(object sender, EventArgs e) 
    { 
     dataGridView2.Visible = false; 
    } 
    DataGridViewRow row; 
    private void dataGridView1_MouseMove(object sender, MouseEventArgs e) 
    { 

     dataGridView2.Visible = true; 
     dataGridView2.Location = new Point(MousePosition.X - 100, MousePosition.Y - 100); 

    } 

    private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (e.RowIndex >= 0) 
     { 
      row = (DataGridViewRow)dataGridView1.Rows[e.RowIndex]; 
     } 

    } 
+0

여기에 내 대답을 참조 http://stackoverflow.com/questions/17771973/usage-of-tooltip-in-c-net-4-0/17772017#17772017 – Ehsan

+0

@EhsanUllah, 귀하의 답변은 목록 상자 용입니다 ... DataGrid? – user2478115

+0

예 아이디어가 동일합니다. 어쨌든 나는 데이터 갤러리 용 답변을 게시했습니다 – Ehsan

답변

1

이 작업을 수행. 눈금의 CellMouseMove 이벤트에 바인딩하십시오.

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (e.RowIndex >= 0) 
     { 
      DataGridViewRow row = DataGridViewRow)dataGridView1.Rows[e.RowIndex]; 
      SqlDataAdapter da2; 
      DataTable dt2 = new DataTable(); 
      da2 = new SqlDataAdapter("SELECT ID, Name, Surname, City FROM tblStudents2 WHERE Name = '" + row["Name"].Value.ToString()+ "'", con); 

      da2.Fill(dt2); 
      dataGridView2.DataSource = dt2; 
     } 
    } 

e.RowIndex 

+0

이 "dgvSessions"는 무엇입니까 ??? – user2478115

+0

코드를 업데이트하고있었습니다. 지금 업데이트되었습니다. – Ehsan

+0

이 부분은 작동하지 않습니다 ... 부적절한 argments :'new Point (new Point (eX, eY))); – user2478115

0

사용 CellMouseMove 이벤트를 찾고있는 인덱스입니다. 당신은 현재 행에 바인딩 된 항목을 얻을 수 있습니다 (를 예를 들어 일부 Person 예) :

void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    if (e.RowIndex < 0) 
     return; 

    DataGridView grid = (DataGridView)sender; 
    var person = (Person)grid.Rows[e.RowIndex].DataBoundItem; 
    var name = person.Name; 
} 
여기

전체 코드입니다 (I 단순 레이블 컨트롤 사용) :

// this field used to avoid loading data which you already have 
private Person currentPerson; 

void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    if (e.RowIndex < 0) 
    { 
     HidePersonDetails(); 
     return; 
    } 

    DataGridView grid = (DataGridView)sender; 
    var person = grid.Rows[e.RowIndex].DataBoundItem as Person; 
    if (person == null) 
    { 
     HidePersonDetails(); 
     return; 
    } 

    var rectangle = grid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); 
    var cellLocation = rectangle.Location; 
    var detailsLocation = new Point(e.X + cellLocation.X, 
            e.Y + cellLocation.Y + rectangle.Height); 
    ShowPersonDetails(person, detailsLocation); 
} 

private void dataGridView1_MouseLeave(object sender, EventArgs e) 
{ 
    HidePersonDetails(); 
} 

private void ShowPersonDetails(Person person, Point location) 
{ 
    if (currentPerson != person) 
    { 
     // get data from SQL server 
     // set datasource of your details grid 
     currentPerson = person; 
    } 

    label1.Text = person.Name; 
    label1.Location = location; 
    label1.Show(); 
} 

private void HidePersonDetails() 
{ 
    label1.Hide(); 
} 
+0

그러나 인덱스를 가져와 Where 절에 넣는 방법 ??? – user2478115

+1

e.Rowindex는 인덱스 – Ehsan

+0

입니다. @ user2478115'DataGridViewCellMouseEventArgs'는 셀 인덱스와 행 인덱스를 모두 제공합니다. –

관련 문제