instead of binding to textboxes how do I link textboxes to data rows or data column in vb.net sqlserver

I discovered that once you problematically bind data to a textbox you can't undo to bind to a new dataset. Someone suggested a better way I hope I describe it right and that is to set text boxes to data rows from a datatable.

This is how I databinded

txtPlayerID.DataBindings.Add("Text", ds.Tables(0), "PlayerID")
        txtFname.DataBindings.Add("Text", ds.Tables(0), "FirstName")
        txtLname.DataBindings.Add("Text", ds.Tables(0), "Lastname")
        txtCollege.DataBindings.Add("Text", ds.Tables(0), "College")
        txtDraftTeam.DataBindings.Add("Text", ds.Tables(0), "DraftTeamID")
        txtHeight.DataBindings.Add("Text", ds.Tables(0), "Height")
        txtHighschool.DataBindings.Add("Text", ds.Tables(0), "HighSchool")
        txtJersey.DataBindings.Add("Text", ds.Tables(0), "Jersey")
        txtNCAAID.DataBindings.Add("Text", ds.Tables(0), "NCAAID")
        txtNcaaTeam.DataBindings.Add("Text", ds.Tables(0), "CollTeamID")
        txtOverall.DataBindings.Add("Text", ds.Tables(0), "OverallPos")
        txtPlayerNote.DataBindings.Add("Text", ds.Tables(0), "BioHTML")
        txtPosDrafted.DataBindings.Add("Text", ds.Tables(0), "DraftPos")
        txtPosition.DataBindings.Add("Text", ds.Tables(0), "Position")
        txtRDDrafted.DataBindings.Add("Text", ds.Tables(0), "Round")
        txtSLUG.DataBindings.Add("Text", ds.Tables(0), "Slug")
        txtStatus.DataBindings.Add("Text", ds.Tables(0), "Status")
        txtTeamID.DataBindings.Add("Text", ds.Tables(0), "TeamID")
        dtp1.DataBindings.Add("Text", ds.Tables(0), "Born")
        txtWeight.DataBindings.Add("Text", ds.Tables(0), "Weight")

        txtYearDrafted.DataBindings.Add("Text", ds.Tables(0), "DraftYear")
        txtYearsinCollege.DataBindings.Add("Text", ds.Tables(0), "YrColl")

Open in new window

 is there a sample I could get using datatables and datarows or columns.
powerztomAsked:
Who is Participating?
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
DataBinding is not the only way to display and retrieve information from a Form. Yes, its simplifies the process for simple things, but you lose a lot of control over the process.

I use DataBinding a lot with grids and ComboBox controls, but when comes the time to display one record at a time in a form, I prefer to do it myself, which gives me a lot of control over what happens between the interface and the data.

I usually have a Form level variable that is a pointer to the current row, let's call it CurrentRow.

When the user moves to a new row, no matter how, I call a method that first saves the content of the CurrentRow to the table, something like:

ds.Tables(CurrentRow).Item("Lastname") = txtLname.Text
ds.Tables(CurrentRow).Item("College") = txtCollege.Text

I then change the value of CurrentRow to reflect the move. If the user requested the following row, I would thus go:

CurrentRow += 1

I then fill the form with the value of the new CurrentRow:

txtLname.Text= ds.Tables(CurrentRow).Item("Lastname")
txtCollege.Text = ds.Tables(CurrentRow).Item("College")

You need to write more code, but you gain complete control, know exactly what is happening and when it is, and can more easily intervene if you need to validate the data or modify it in any way before committing it to the table.

This being said, if you still want to use DataBinding and want to use a DataTable instead of a DataSet:

txtLname.DataBindings.Add("Text", YourTable(0), "Lastname")
txtCollege.DataBindings.Add("Text",YourTable(0), "College")

As for the DataRow and the column, they are already there. (0) is the first row of the table in that code. Simply change the value if you want another row. "LastName" and "College" are the columns.
0
 
powerztomAuthor Commented:
Hey thanks for the detailed explanation. It helped me out in understanding manipulation for data.Cheers
0
 
powerztomAuthor Commented:
Yo James I defined Currentrow as a integer

and tried to incorporated
ds.Tables(CurrentRow).Item("Lastname") = txtLname.Text
ds.Tables(CurrentRow).Item("College") = txtCollege.Text
and
txtLname.Text= ds.Tables(CurrentRow).Item("Lastname")
txtCollege.Text = ds.Tables(CurrentRow).Item("College")

but I got a error line under item I just need to fill datagridview with different datasets or data tables (different queries) and have textboxes display each move on the datagridview. Maybe I 'm missing something ?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Jacques Bourgeois (James Burger)PresidentCommented:
When you are asking a question about an error, it is necessary that you tell us what the error is and exactly on which line you get the error. Since a lot of things can cause an error, it is usually impossible for us to help you correctly without that information.

Depending on how your project is set, as well as the type of data that is in the table, you might need to convert the result of calling Item to String in order to use it in the Text property:

txtLname.Text= CStr(ds.Tables(CurrentRow).Item("Lastname"))

But since you did not specify what the error is, it can be something else.

And you never told us about the grid.

Since you want to follow the DataGridView, do not fill your TextBoxes from the DataTable, do it from the grid.

Depending on different factors, information in the grid and in the table can be slightly different. This could confuse the user.

If the grid is not sorted the same way as the DataTable, CurrentRow becomes useless, because what is row 5 in the grid might not be the same information that you have in row 5 of the table.

The following might be more appropriate than trying to get the data from the DataTable:

txtLname.Text= yourGrid.CurrentRow.Cells("Lastname")  or
txtLname.Text=CStr(yourGrid.CurrentRow.Cells("Lastname")) if it does not work

Depending on how the grid was filled in, you might also not be able to retrieve the information it contains through the name of the field. If this is the case, then you will have to use the index (position) of the column instead:

yourGrid.CurrentRow.Cells(3)
0
 
powerztomAuthor Commented:
Never mind Dude I got it Thanks
0
 
powerztomAuthor Commented:
Thanks again James for Input I think I got it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.