Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB2005 Pointer on Data Binding.

Status
Not open for further replies.

beautieee

Programmer
Oct 4, 2008
46
MY
Im new to VB 2005. Seeking for kind advise.

With refer to my code below, why is my pointer always refering to record no.1 on textbox1.text? No matter which record i clicked, i will still getting data of username from first record. Anything i Missed out?

Curiously to ask, is that possible to apply .addnew command and .update command rather than using SQL command of "Update username ..." with using below namespace.

Thank you in advance.

Regards,
Beautieee.

Code:
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Dim objConnection As New SqlConnection(ConnectT)

    Dim objDataAdapter As New SqlDataAdapter()
    Dim objDataSet As New DataSet()
    Dim objDataView As DataView
    Dim objCurrencyManager As CurrencyManager
    Dim objCommand As New SqlCommand()


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the SelectCommand properties...
        objConnection.Open()
        objDataAdapter.SelectCommand = New SqlCommand()
        objDataAdapter.SelectCommand.Connection = objConnection
        objDataAdapter.SelectCommand.CommandText = "SELECT * from usertable"

        objDataAdapter.SelectCommand.CommandType = CommandType.Text
        ' Open the database connection...
        'objConnection.Open()
        ' Fill the DataSet object with data...
        objDataAdapter.Fill(objDataSet, "usertable")

        objDataView = New DataView(objDataSet.Tables("usertable"))
        objCurrencyManager = _
CType(Me.BindingContext(objDataView), CurrencyManager)

        ' Close the database connection...
        'objConnection.Close()
        ' Set the DataGridView properties to bind it to our data...
        grdAuthorTitles.AutoGenerateColumns = True
        grdAuthorTitles.DataSource = objDataSet
        grdAuthorTitles.DataMember = "usertable"
        ' Clean up
        'objDataAdapter = Nothing
        'objConnection = Nothing
    End Sub

    Private Sub grdAuthorTitles_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdAuthorTitles.CellDoubleClick
        TextBox1.DataBindings.Clear()
        TextBox1.DataBindings.Add("Text", objDataView, "username")      
    End Sub
 
Try moving these lines:

TextBox1.DataBindings.Clear()
TextBox1.DataBindings.Add("Text", objDataView, "username")

out of the grdAuthorTitles_CellDoubleClick event handler and put them at the bottom of the Form_Load event handler.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Jebenson, the trick didnt work. the textbox1.text always stick to first record. How do i display textbox1.text with correct data whenever i move my cursor to other record? Any other idea?

Regards,
Beautieee
 
Jebenson is correct--you don't need to redo the binding in the double click event. However, your issue is due to the fact that your grid and your text box are bound to different sources--one to a DataTable and the other to a DataView. If you bind them to the same source, and set the Textbox's databinding up there in the Form Load event, you should be good to go.
 
Thank you RiverGuy. Finally it worked as expected.

Change the datasourse to dataview and disabled the datamember.

Code:
grdAuthorTitles.DataSource = objDataView
        'grdAuthorTitles.DataMember = "usertable"
        ' Clean up
        'objDataAdapter = Nothing
        'objConnection = Nothing
        TextBox1.DataBindings.Clear()
        TextBox1.DataBindings.Add("Text", objDataView, "username")

Regards,
Beautieee
 
With refer to above code, why was the data edited but wont be updated in datagridview? Propertise set to allowedit, readonly false. Any additionaly coding needed?

Please advise.

Thank you.

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top