CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012

    Post How to prevent firing RowValidate/Validating event before a function

    Hi, there. I designed a DataGridView under the name "dgvTarifa" and took a chance on trying Row Validating (tried Validated, CellEnter, CellLeave, RowEnter, RowLeave all count same) event. The purpose of the code is to load the selected values of the respective columns into three textboxes in order to rephrase/revalue those and update them by pressing a button. All works fine every single line of code there's nothing wrong except one logical thing.

    When the form loads the RowValidating automatically fires because I have a function that Selects the table from the Database and it fills the DataSet by then attaching it to the datasourse of the "dgvTarifa" table. After setting the datasource of the datagridview, it automatically jumps to the RowValidating event which I DONT WANT TO! It first has to finish the whole function and later when the user enters with mouse or enters/leaves a row this has to be fired.
    (eventually already have the CellClick event with the same code below so only RowValidating has to work properly...)

    Here's the code I use at row validating
    Private Sub dgvTarifa_RowValidating(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvTarifa.RowValidating
            If e.RowIndex < 0 Then
                Exit Sub
                lblID.Text = Me.dgvTarifa.Rows(e.RowIndex).Cells("TariffID").Value
                txtCName.Text = dgvTarifa.Rows(e.RowIndex).Cells("CountryName").Value.ToString
                txtCCode.Text = dgvTarifa.Rows(e.RowIndex).Cells("CountryCode").Value.ToString
                txtCPrice.Text = dgvTarifa.Rows(e.RowIndex).Cells("CountryPrice").Value.ToString
            End If
        End Sub
    And this is the Select function of the table which fires at Form_Load of the form:
     Function SelectTariff()
                Dim cmd1 As SqlCommand = New SqlCommand("SELECT * FROM SMDR_Tariff ORDER BY TariffID DESC", con) 'Declare cmd1 as SQL Server Command "SELECT" which loads all columns and rows throughout Microsoft SQL Connection "conn".
                Dim ds As New DataSet
                Dim da As New SqlDataAdapter
                da.SelectCommand = cmd1 ' Data Adapter(da) command selection is cmd2, which means selects all cells from the specific table declared above throughout SQL Connection "conn".           
                da.Fill(ds, "SMDR_Tariff") 'Data Adapter(da) fills, adds or refreshes cells or rows in the Data Set(ds).
                Me.dgvTarifa.DataSource = ds.Tables(0) 'Sets MDI Parent(this form) data source of the Data Grid View "dgvTarifa" from the first(0) indexed table of Data Set (ds).
                dgvTarifa.GridColor = Color.DarkSlateGray
                dgvTarifa.CellBorderStyle = DataGridViewCellBorderStyle.None
                dgvTarifa.BackgroundColor = Color.LightGray
                dgvTarifa.DefaultCellStyle.SelectionBackColor = Color.RoyalBlue
                dgvTarifa.DefaultCellStyle.SelectionForeColor = Color.White
                Me.dgvTarifa.DefaultCellStyle.ForeColor = Color.Ivory
                dgvTarifa.DefaultCellStyle.WrapMode = DataGridViewTriState.[True]
                dgvTarifa.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                dgvTarifa.RowsDefaultCellStyle.BackColor = Color.Silver
                dgvTarifa.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray
                dgvTarifa.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                Me.dgvTarifa.Columns("Delete").DisplayIndex = 4
            Catch ex As Exception
                MsgBox(ex.Message & vbCritical) 'Catch exception/s that might occurr anytime by any reason into a simplified message box followed by a critical sign within.
            End Try
            Return 0
        End Function
    What are the very best suggestions regarding this issue? Is there any other event I can use instead of the above mentioned. Or how can I stop jumping to this event before the function has been processed to the end?

  2. #2
    Join Date
    Aug 2009
    NW USA

    Re: How to prevent firing RowValidate/Validating event before a function

    If you used a BindingSource you could key off of those events which do not fire as often as dgv events. The only other thing that has worked for me it to define a public flag (e.g. "bLoading") and set it to true when you start SelectTariff and false when you are done. You then put an If statement in your dgv event to see if it's still loading or done. It is not what I would call elegant but sometimes you have to get it done.

  3. #3
    Join Date
    Feb 2012

    Re: How to prevent firing RowValidate/Validating event before a function

    Thanks that really worked out for me beside the function I had to put several True/False boolean values in other subs/events aswell to prevent the RowValidating and in fact switched back to CellValidated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.