Validate (Primary Key Constrain) for DataGridView

Dear All,

I have a problem when I want to active check validate primary key value in DataGridView.
EX : I have a DataTable with ID is Primary Key Column.
When I stay at Add Mode, I deliberate to input duplicate value or wrong ID's value and then i move previous row, this new row is remove (or delete). This thing is similar when i stay at Edit Mode, it returns the origrinal value.

My target is active check validate such as : show my message "Duplicate Value" or "This value is not valid".

If possible, give me sample project (VS.NET 2005) to solve my problem.

Thanks for your cooperation.



Answer this question

Validate (Primary Key Constrain) for DataGridView

  • lildan

     khiemvo wrote:

    Dear All,

    I have a problem when I want to active check validate primary key value in DataGridView.
    EX : I have a DataTable with ID is Primary Key Column.
    When I stay at Add Mode, I deliberate to input duplicate value or wrong ID's value and then i move previous row, this new row is remove (or delete). This thing is similar when i stay at Edit Mode, it returns the origrinal value.

    My target is active check validate such as : show my message "Duplicate Value" or "This value is not valid".

    If possible, give me sample project (VS.NET 2005) to solve my problem.

    Please help me...... i waiting for a long time

    Thanks for your cooperation.


  • JeromeGab

    We have two levels of support for this. One you can use the CellValidating event and when the cell's value is dirty you can check that the cell's value (your unique ID value) is a valid value. If not you can set the Cancel property in the event args to true. Do note that CellValidating occurs when you move to another cell in the same row.

    You can also use the RowValidating event and perform your validating there. The nice thing about RowValidating is that this event occurs when you move to a different row, but not while you are in the same row.

    One important difference is that canceling RowValidating event will not keep the cell in edit mode, but canceling CellValidating does keep the cell in edit mode.

    -mark
    DataGridview Program Manager
    Microsoft
    This post is provided "as-is"

  • Validate (Primary Key Constrain) for DataGridView