Question fill textbox in datagridview based on combobox value in datagridview

Mimosa777

Active member
Joined
Aug 28, 2014
Messages
28
Programming Experience
1-3
hi there,

hopefully the title is clear enough - what im trying to achieve is create an invoice form where the user can fill in the cells in the datagridview like Qty, Code, Description and price.
Qty is a textbox - user enter value manually
Code is a combobox - user pick a value from the list (combobox is bind to a datasource)
Description is a textbox - fill automatically based on combobox selection (code) - bind to same datasource
Price is a textbox - fill automatically based on combobox selection (code) - bind to same datasource

here is my code - im able to select the value in the combobox, but the description and price are not populating. Any idea why ?
VB.NET:
Dim bindingpart As New BindingSource()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|dbServauto.mdb"
        con.Open()
        sqlP = "SELECT * from tblParts"
        daParts = New OleDb.OleDbDataAdapter(sqlP, con)
        daParts.Fill(dspart, "Parts")
        bindingpart.DataSource = dspart.Tables("Parts")
        'dgFactures.DataSource = bindingpart
        con.Close()
End sub
VB.NET:
Private Sub dgFactures_SelectionChanged(sender As Object, e As EventArgs) Handles dgFactures.SelectionChanged
        With colCode
            .DataSource = bindingpart
            .DisplayMember = "Code"
            .ValueMember = "Code"
        End With

'retrieve data from bindingsource for the textbox Description and Price
        colDesc.DataGridView.DataBindings.Add(New Binding("text", bindingpart, "Description"))
        colDesc.DataGridView.DataBindings.Clear()
        colPrice.DataGridView.DataBindings.Add(New Binding("text", bindingpart, "Price"))
        colPrice.DataGridView.DataBindings.Clear()

End Sub

Thank you for your help
 
You aren't actually binding what you think you are. Look at these lines:
VB.NET:
        [B][COLOR="#FF0000"]colDesc.DataGridView.DataBindings.Add(New Binding("text", bindingpart, "Description"))[/COLOR][/B]
        colDesc.DataGridView.DataBindings.Clear()
        [B][COLOR="#FF0000"]colPrice.DataGridView.DataBindings.Add(New Binding("text", bindingpart, "Price"))[/COLOR][/B]
        colPrice.DataGridView.DataBindings.Clear()
In the case of the two highlighted lines, you presumably think that you are binding the Text properties of the TextBoxes in the cells of those columns. Not even close I'm afraid. There's no reference at all to the cells in the columns or the TextBoxes in the cells there. You're not even binding properties of the columns. You are binding the Text property of the DataGridView itself. Not only that, you are unbinding it immediately after so, even if that was useful, you would be losing that usefulness.

Basically, you can't use data-binding like that. You're going to have to handle the CellValueChanged event of the grid, detect a change in the Code column and then make the corresponding change in the Description and Price columns yourself.
 
i have rework my code based on your last comment as below :
VB.NET:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|dbServauto.mdb"
        con.Open()
        sqlP = "SELECT * from tblParts"
        daParts = New OleDb.OleDbDataAdapter(sqlP, con)
        daParts.Fill(dspart, "Parts")
        bindingpart.DataSource = dspart.Tables("Parts")
        'dgFactures.DataSource = bindingpart
        con.Close()
        With colcode
            .DataSource = bindingpart
            .DisplayMember = "Code"
            .ValueMember = "Code"
        End With
    End Sub



Private Sub dgFactures_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgFactures.CellValueChanged
        Try
            dgFactures.Rows(dgFactures.CurrentRow.Cells(0).RowIndex).Cells(2).Value = bindingpart.Current("Description").ToString
            dgFactures.Rows(dgFactures.CurrentRow.Cells(0).RowIndex).Cells(3).Value = bindingpart.Current("Price").ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

The only issue I have right now is that when I launch my form, I got an error : "Object Reference not set to an instance of an Object"
When I bypass the exception error, The description and price cell are filled in correctly. I've tried to debug the error but with no success. I don't see why i'm having this error above. Any idea ? thank you
 
Last edited:
works perfectly as I wanted like this using the CurrentCellDirtyStateChanged event :
VB.NET:
Private Sub dgFactures_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgFactures.CurrentCellDirtyStateChanged
        Try
            dgFactures.Rows(dgFactures.CurrentRow.Cells(0).RowIndex).Cells(2).Value = bindingpart.Current("Description").ToString
            dgFactures.Rows(dgFactures.CurrentRow.Cells(0).RowIndex).Cells(3).Value = bindingpart.Current("Price").ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
 
Last edited:
dgFactures.Rows(dgFactures.CurrentRow.Cells(0).RowIndex) is the same as dgFactures.CurrentRow
 
Back
Top