简体   繁体   English

在DataGridView中,根据输入的值更改选定的单元格

[英]In a DataGridView, change selected cell based on value entered

In my datagridview i have the following columns: 在我的datagridview中,我有以下几列:

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. 如果用户在我的DataGridView中输入有效的零件号,我希望焦点直接移至“销售数量”列,以便用户可以输入他们正在订购的数量。

I realize this code can change the selected cell in the datagridview: 我意识到这段代码可以更改datagridview中的选定单元格:

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

but under what event of the datagridview do I put this code? 但是在datagridview的什么情况下我应该放置这段代码? 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. 我的另一个问题是,如果用户在输入有效零件之前单击或尝试使用Tab键或箭头将其转到“已售出数量”列,我希望程序将用户移回“零件列”单元格。 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. 现在我意识到我可以阻止用户进入任何其他单元,直到使用CellValidating事件输入有效零件为止(e.cancel = true,除非输入了有效零件),但是如果没有输入零件编号并且用户改为尝试移动到具有有效零件信息的前一行时,CellValidating事件下的代码将无法工作,因为当您更改单元格时,CellValidating事件不知道您要更改为哪个单元格...因此,我可以t编写代码以考虑用户尝试将焦点移到的位置。 I have worked on this problem for a solid week (atleast 40+hours) please help. 我已经在这个问题上工作了整整一周(至少40个小时以上),请帮忙。

For the second problem, 对于第二个问题

you could set the e.Cancel=true only if Part number is not empty. 仅当部件号不为空时才可以设置e.Cancel=true Add the below code in the CellValidating event handler 在CellValidating事件处理程序中添加以下代码

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

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

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