C# – How to Trigger Datagridview.cellValidating manually

cdatagridviewnetvb.net

I am inserting few records in my Datagridview from another table, I want to call CellValidating event for every record I insert. Please help me out.

Here is my code

            m_Sqlstr = "Select Distinct RFID, Prod_Code, Lot_No From  ScanStk Where BatchNo = '" & strBatchno & "' "
            m_SqlCmd = New SqlCommand(m_Sqlstr, Con)
            Con.Open()
            RFIDReader = m_SqlCmd.ExecuteReader(CommandBehavior.CloseConnection)

            With m_BindingsrcDetail
                    While RFIDReader.Read
                        .AddNew()
                        .Current("SR") = m_BindingsrcDetail.Count
                        .Current("LOT_NO") = RFIDReader("LOT_NO").ToString.Trim
                        .Current("PROD_CODE") = RFIDReader("PROD_CODE").ToString
                        .Current("QUANTITY") = 1
                        GrdDetails.BeginEdit(False)
                        GrdDetails.CurrentRow.Cells("Prod_code").Value = RFIDReader("PROD_CODE").ToString
                        GrdDetails.EndEdit()
                    End While
            End With

grddetails is the name of grid,
m_BindingsrcDetail is datasource for grdDetails

According to This , You should be able to force the validation trigger using BeginEdit and EndEdit. but its not happening.

I am fetching Product description, MRP and other details in cell validating. So it necessary to call it.

Best Answer

You have to change the current cell and then go back to it. Give this a try. You may have to alter it, as it hasn't been tested.

 BeginEdit();
 DataGridViewCell currentCell = GrdDetails.CurrentRow.Cells("Prod_code");
 EndEdit();
 CurrentCell = null;
 CurrentCell = currentCell;