-1

我在一个表格中有 2 个网格。一个网格充满了一些学生信息。我希望当我将鼠标放在 dataGridview1 上时,显示另一个网格视图,如 Popup 或类似的东西,并使用基于 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 Statement 写什么,在:WHERE Name=' _ __ _ ' ??

我希望当 Mous 超过第一行时,获取名称(列索引 1)并填充另一个数据网格。

我希望你能理解我 :(

*** *已编辑这是我通过 Ehsan 代码后的代码......它几乎可以工作。问题是在将鼠标移动到下一行后它不会重新填充网格!!但是,如果我离开 DataGrid,并将鼠标移到另一行,它会显示该行的信息。将鼠标移动到下一行后如何制作类似刷新网格的东西?

 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]; 
        }

    }
4

2 回答 2

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

是您要查找的索引

于 2013-07-22T13:05:53.583 回答
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;
}

这是完整的代码(为简单起见,我使用标签控件):

// 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();
}
于 2013-07-22T13:09:35.430 回答