简体   繁体   English

在C#Winform中在DataGridView活动行下方设置ListView的位置

[英]Set Location of ListView below DataGridView Active Row in C# Winform

Tools: Visual Studio 2010 Ultimate, Language C#, Database MySql 工具:Visual Studio 2010 Ultimate,语言C#,数据库MySql

Hi, 你好

I am searching this a while, but to date didn't find any suitable solution. 我搜索了一阵子,但到目前为止找不到任何合适的解决方案。

I've a form with bound DataGridView with 5 columns, ProductID, ProductName, Qty, Price and Amount. 我有一个绑定DataGridView的表单,其中包含5列,即ProductID,ProductName,Qty,Price和Amount。 After normal data entry user can click button to save data to Mysql database, no problem there. 普通数据输入后,用户可以单击按钮将数据保存到Mysql数据库,那里没有问题。

At present user is manually enter ProductID after which an Sql command is executed to fetch ProductName and sets it to DataGridView ProductName Column. 目前,用户手动输入ProductID,然后执行Sql命令以获取ProductName并将其设置为DataGridView ProductName列。 I need a way to show a ListView below ProductID column when it got focus, so that user can select product (ProductID and ProductName) from ListView and set it to DataGridView's Row which user is currently using. 我需要一种在获得焦点时在ProductID列下方显示ListView的方法,以便用户可以从ListView中选择产品(ProductID和ProductName)并将其设置为用户当前正在使用的DataGridView的Row。

Is it possible to show listview below ProductID cell when user click or when got focus? 当用户单击或获得焦点时,是否可以在ProductID单元格下面显示列表视图?

Thanks 谢谢

Ahmed 艾哈迈德

Try the code below, I think it's a good start - but definitely need improvements. 试试下面的代码,我认为这是一个不错的开始-但绝对需要改进。 But please not that as I mentioned in my comment above, you should treat special cases such as scrolling or even expanding the grid so the ListView will not hide DataGridView rows. 但是请不要像我在上面的评论中提到的那样,应该处理特殊情况,例如滚动甚至扩展网格,以便ListView不会隐藏DataGridView行。

On your DataGridView subscribe the MouseMove event and store the mouse location: 在您的DataGridView订阅MouseMove事件并存储鼠标位置:

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
    mouseLoc = e.Location;
}

You can set mouseLoc as a property. 您可以将mouseLoc设置为属性。

private Point mouseLoc = new Point();

And also subscribe to CellMouseDown event, with this code: 并使用以下代码订阅CellMouseDown事件:

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    listView1.Location = new Point(mouseLoc.X, mouseLoc.Y + dataGridView1[e.ColumnIndex, e.RowIndex].Size.Height);
    listView1.BringToFront();
    listView1.Refresh();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM