1

I was working with VB.NET and SQL Server database. I want to display selected row into the textbox so that I can edit them and update to database. But when I finished my code, it comes out an exception told me the column name is not found. I double checked the column name and confirm I was spelling correct. What is the problem? Here is my code:

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)

        TextBox1.Text = row.Cells("StudentID").Value.ToString
        TextBox2.Text = row.Cells("StudentName").Value.ToString
        TextBox3.Text = row.Cells("HomeAddress").Value.ToString
        TextBox4.Text = row.Cells("ContactNumber").Value.ToString
        TextBox5.Text = row.Cells("SubjectCode").Value.ToString
        TextBox6.Text = row.Cells("SubjectName").Value.ToString
        TextBox7.Text = row.Cells("ParentName").Value.ToString
        TextBox8.Text = row.Cells("ParentContact").Value.ToString
    End If
End Sub

Here is my database:

Database

Also I want to ask how can I update/refresh my database by using a button? I look through online but nothing effective, I want to refresh the database after I add/edit/delete a data.

Thank you very much.

3
  • chek is your columns name case-sensitive Commented Apr 9, 2016 at 14:26
  • @dejan87 But I wrote exactly like my column name... Commented Apr 9, 2016 at 14:33
  • I keep getting this error: An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll Additional information: Column named StudentID cannot be found. Commented Apr 9, 2016 at 14:34

2 Answers 2

1

You're going about this the wrong way. I'm guessing that you are populating a DataTable from the database. You should then be binding that DataTable to a BindingSource and you should then be binding that to the DataGridView AND the TextBox controls. The TextBox controls will then automatically be populated when you select a grid row AND any changes you make in the TextBox controls will be automatically pushed back to the DataTable and the grid.

To bind a TextBox, select it in the designer and then open the Properties window. You can then expand the (DataBindings) node and select the Text property under that. From there you can select the BindingSource as the data source and specify which column to bind to.

Sign up to request clarification or add additional context in comments.

2 Comments

is it this one ? Private Sub Edit_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Student_InfoTableAdapter1.Fill(Me.Tuition_InformationDataSet6.Student_Info) End Sub
That is the Load event handler for the form and, in that event handler, you appear to be populating the appropriate DataTable from the database. Given that you have no code to bind the grid there, I'm guessing that the binding is done in the designer. Did you drag that table from the Data Sources window to create the grid? If so then it will have added the BindingSource and created the binding too. You can now bind the TextBox controls in the designer too. See my edit above to learn how to do that.
0

Use this:

Private Sub dgvEmployees_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployees.CellContentClick

textbox1.Text = datagridview.Rows(e.RowIndex).Cells(0).Value

end sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.