开发者

currentCell to remain the same if validation fail. - CellEndEdit Event

开发者 https://www.devze.com 2023-03-02 00:47 出处:网络
I have a datagridview for user\'s input. For example, the columns are \"Name\", \"Date of Birth\"... For the \"Date of Birth\" part, i have implemented validaion...

I have a datagridview for user's input.

For example, the columns are "Name", "Date of Birth"...

For the "Date of Birth" part, i have implemented validaion...

i want to make it that user have to enter a valid "Date of birth" before they are allowed to leave the Cell. Meaning once them entered the Cell, they have to provide a valid "Date of birth" then they can move on.

开发者_StackOverflow中文版

would like to find out how this can be done, i have tried setting the DGV.currentCell to the intended cell inside "CellEndEdit" event. But it gives me the following error: "operation is not valid because it results in a reentrant call to the setcurrentcelladdresscore function"


I believe you can achieve this by using CellValidating

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if(e.ColumnIndex == 3)
    {
       e.FormattedValue // Check your date validation against this value
       e.Cancel = true; // set this to true if validation fails
    }
}

This will help in retaining the focus in the same cell in case of an incorrect entry by the user.

0

精彩评论

暂无评论...
验证码 换一张
取 消