简体   繁体   中英

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

Tools: Visual Studio 2010 Ultimate, Language C#, Database 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. After normal data entry user can click button to save data to Mysql database, no problem there.

At present user is manually enter ProductID after which an Sql command is executed to fetch ProductName and sets it to DataGridView ProductName Column. 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.

Is it possible to show listview below ProductID cell when user click or when got focus?

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.

On your DataGridView subscribe the MouseMove event and store the mouse location:

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

You can set mouseLoc as a property.

private Point mouseLoc = new Point();

And also subscribe to CellMouseDown event, with this code:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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