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

I see there is no response to this. I'm going to make it more clear, and this is code for save_edit button, where I can actually catch surrent row and stay on it. The problem is, when user add new data, in that particular period app doesn't know what row it will be. That is why I'm trying to get that row by catching textbox input value.

This is sample with save_edit button, and it works great.

Dim selRow As Integer
        Dim selCol As Integer
        If TextBox1.Text = "" Or String.IsNullOrEmpty(TextBox1.Text) Then
            MessageBox.Show("Type some data")
        Else
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand("Updatestoredprocedure", con)
            cmd.Parameters.AddWithValue("@SomeData", (TextBox1.Text))
            cmd.Parameters.AddWithValue("@MoreData", (TextBox2.Text))
            cmd.Parameters.AddWithValue("@LastData", (TextBox3.Text))
            cmd.Connection = con
            cmd.CommandType = CommandType.StoredProcedure
            Try
                If DataGridView1.SelectedCells().Count > 0 Then
                    selRow = DataGridView1.CurrentCell.RowIndex
                    selCol = DataGridView1.CurrentCell.ColumnIndex
                End If
                Dim rdr As SqlDataReader = cmd.ExecuteReader
                Dim dt As New DataTable
                dt.Load(rdr)
                rdr.Close()
                DataGridView1.DataSource = dt
                con.Close()
                DataGridView1.ClearSelection()
                '   DataGridView1.Rows(selRow).Cells(selCol).Selected = True
                DataGridView1.CurrentCell = DataGridView1.Rows(selRow).Cells(selCol)

            Catch ex As SqlException
                MessageBox.Show(ex.Message.ToString(), "Error Message")
            End Try
        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.