April 4th, 2012, 10:26 PM
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
And this is the Select function of the table which fires at Form_Load of the form:
Private Sub dgvTarifa_RowValidating(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvTarifa.RowValidating
If e.RowIndex < 0 Then
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
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?
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.
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!