简体   繁体   中英

In a DataGridView, change selected cell based on value entered

In my datagridview i have the following columns:

Part, Description, Qty Sold, Qty Lost, Sell Price

If a user enters a valid part number in my DataGridView, I want the focus to move directly to the the qty sold column so that the user can enter the qty they are ordering.

I realize this code can change the selected cell in the datagridview:

dgvPOS.Rows(intCurrentRow).Cells("ordQtySold").Selected = True

but under what event of the datagridview do I put this code? If a user types the valid part and then hits the tab button, I'm running into the problem of the focus going to the next column which is the Description column despite the code mentioned above that is suppose to move it to the qty sold column.

My other issue is if a user clicks or attempts to tab or arrow over to the the qty sold column before the user has entered a valid part, i want the program to move the user back to the part column cell. Now I realize I could stop the user from going to any other cell until a valid part is entered using the CellValidating event (e.cancel = true unless a valid part is entered), however if no part number is entered and the user instead is trying to move to a previous row that has valid part information, the code under the CellValidating event will not work because when you change cells the CellValidating event does not know what cell you are attempting to change to... so that means I can't write code to factor where the user is trying to move the focus to. I have worked on this problem for a solid week (atleast 40+hours) please help.

For the second problem,

you could set the e.Cancel=true only if Part number is not empty. Add the below code in the CellValidating event handler

if(e.FormattedValue != null && !e.FormattedValue.ToString().IsNullOrEmpty())
{
      e.Cancel = true;
}

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