Question Question about unbound DataGridView

XiaoHong

New member
Joined
May 22, 2013
Messages
2
Programming Experience
Beginner
Dear All,

I'm very new in VB.NET.
I'm now facing a problem on developing unbound datagridview.
My datagridview consist of 2 column - Column A and column B.

MY problem is when user key in new record on column A and pressed "Tab" or "Enter" button, then i would like the program to check for duplicate column A value. if column A value existed, then i want the program to prompt message to inform user the new value of column A already exist , and then the cursor must focus on column A. How can i do that?

And when user key in new record on column A, if column A value not exist, then i want the cursor to focus on column B instead of next record of column A. How can i do that?

Can anyone help me on it?

Thank you very much!
 
MY problem is when user key in new record on column A and pressed "Tab" or "Enter" button, then i would like the program to check for duplicate column A value. if column A value existed, then i want the program to prompt message to inform user the new value of column A already exist , and then the cursor must focus on column A. How can i do that?
You want the CellValidating event of the grid. You can get the Value of the current cell and then loop through the rows and compare it to the Value of each cell in the same column. If you find a match then display a message to the user and set e.Cancel to True, which will prevent that cell losing focus.
And when user key in new record on column A, if column A value not exist, then i want the cursor to focus on column B instead of next record of column A. How can i do that?
The Tab key already does exactly that, so if that's what the user wants then they should press the Tab key.
 
Hi jmcilhinney,

Thanks for yout reply!
i solved 1st issue, but one things is if user move from column A to column B with null value of column A. how can i let the cursor to focus on column A before they can enter value into column B ( for new record)?

For issue no2, when user press "Enter" button on column A for new record, can the program behaviour like press "Tab" button?
 
i solved 1st issue, but one things is if user move from column A to column B with null value of column A. how can i let the cursor to focus on column A before they can enter value into column B ( for new record)?
You're already handling the CellValidating event. If part of validating the cell is making sure that it contains a value then make sure it contains a value in the CellValidating event handler. I already told you how to prevent the user leaving that cell.
For issue no2, when user press "Enter" button on column A for new record, can the program behaviour like press "Tab" button?
It can with a bit of code but I don't see the point so I'm not going to try to work out what that code is, but I've seen that same question asked before so a search should turn it up. It's worth noting that a DataGridView works just like Excel with respect to Enter and Tab, i.e. Enter moves down one row and Tab moves across one column. As such, that's what most users would expect.
 
Back
Top