Hi guys, I have one question about Visual Basic and DataGridView behaviour.
So, after I type all the text boxes and click on button (save data), I would like user to get focus on that new data. Now, each time button is clicked, DataGridView goes on a first row. New data can be on many positions, depending on user input.
This is my code, and I wonder if there is a way to catch TextBox1 value, and after button completes its tasks to find apropriate row based on a TextBox1 value.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Dim inn As Integer = Convert.ToInt32(TextBox1.Text)
Dim test As Boolean = False
For Each row In DataGridView1.Rows
If TextBox1.Text = Trim(row.Cells("NewEntry").Value.ToString) Then
test = True
MsgBox("Double entry, try with a new one")
TextBox1.Focus()
TextBox1.BackColor = Color.Red
End If
Next
If test = False Then
If TextBox1.Text = "" Or String.IsNullOrEmpty(TextBox1.Text) Or TextBox2.Text = "" Or String.IsNullOrEmpty(TextBox2.Text) Or TextBox3.Text = "" Or String.IsNullOrEmpty(TextBox3.Text) Then
MessageBox.Show("Check for all data")
Else
con.Open()
Dim cmd As SqlCommand = New SqlCommand("Insertjmj", con)
cmd.Parameters.AddWithValue("@NewEntry", Trim(TextBox1.Text))
cmd.Parameters.AddWithValue("@NewMark", Trim(TextBox2.Text))
cmd.Parameters.AddWithValue("@NewDescription", Trim(TextBox3.Text))
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
Try
Dim rdr As SqlDataReader = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(rdr)
rdr.Close()
DataGridView1.DataSource = dt
con.Close()
' DataGridView1.CurrentCell = DataGridView1.Rows(inn).Cells(0)
Catch ex As SqlException
MessageBox.Show(ex.Message.ToString(), "Error Message")
End Try
End If
End If
End Sub