|
-
Sep 26th, 2007, 08:47 AM
#1
Thread Starter
Hyperactive Member
Fill textboxes by clicking on DGV using BindingSource
From this thread I kind of get the idea of how to fill textboxes with records from a row by clicking on the datagridview but I have not quite got it right in practice.
My Code:
VB Code:
Private Sub frmEdit_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim da As SqlDataAdapter
Dim ds As DataSet = New DataSet
da = New SqlDataAdapter("Select * " & _
"From vShowEditData Where StoreID = '" & _
StoreID & "' Order by CategoryCode, ItemCode", Con)
da.TableMappings.Add("Table", "EditInv")
Try
da.Fill(ds)
ds.Tables("EditInv").DefaultView.AllowNew = False
dgvEditData.DataSource = ds.Tables("EditInv")
myBindingSource.DataSource = ds.Tables("EditInv")
Catch ex As Exception
MessageBox.Show( _
ex.Message & vbNewLine & _
ex.Source, "Data Error", _
MessageBoxButtons.OK)
Finally
da.Dispose()
ds.Dispose()
End Try
End Sub
Private Sub dgvEditData_RowHeaderMouseDoubleClick( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles dgvEditData.RowHeaderMouseDoubleClick
tbStart.DataBindings.Add("Text", myBindingSource, "OnHandQ")
End Sub
On the first dbl Click it sets it to 0 every time and on the second dbl Click I get this error: This causes two bindings in the collection to bind to the same property. Parameter name: binding
-
Sep 26th, 2007, 08:53 AM
#2
Re: Fill textboxes by clicking on DGV using BindingSource
This is wrong:
vb.net Code:
dgvEditData.DataSource = ds.Tables("EditInv")
myBindingSource.DataSource = ds.Tables("EditInv")
It should be like this:
vb.net Code:
myBindingSource.DataSource = ds.Tables("EditInv")
dgvEditData.DataSource = myBindingSource
Also, you should be setting the AllowNew property of the BindingSource, not the DefaultView.
If you want your TextBoxes, etc., to be populated with the same record's data when you select a row in the grid then you need to bind those TextBoxes, etc., to the same BindingSource as the grid.
-
Sep 26th, 2007, 09:17 AM
#3
Thread Starter
Hyperactive Member
Re: Fill textboxes by clicking on DGV using BindingSource
Got it. Thanks. The only issue I have now is that if I select a second row I get the error mentioned above. Do I need to clear the databinding before I collect the row data (each time)? I tried DataBindings.Clear() But I get the same error.
VB Code:
Private Sub frmEdit_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim da As SqlDataAdapter
Dim ds As DataSet = New DataSet
da = New SqlDataAdapter("Select * " & _
"From vShowEditData Where StoreID = '" & _
StoreID & "' Order by CategoryCode, ItemCode", Con)
da.TableMappings.Add("Table", "EditInv")
Try
da.Fill(ds)
myBindingSource.DataSource = ds.Tables("EditInv")
dgvEditData.DataSource = myBindingSource
myBindingSource.AllowNew = False
Catch ex As Exception
MessageBox.Show( _
ex.Message & vbNewLine & _
ex.Source, "Data Error", _
MessageBoxButtons.OK)
Finally
da.Dispose()
ds.Dispose()
End Try
End Sub
Private Sub dgvEditData_RowHeaderMouseDoubleClick( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles dgvEditData.RowHeaderMouseDoubleClick
DataBindings.Clear()
tbStart.DataBindings.Add("Text", myBindingSource, "OnHandQ")
End Sub
-
Sep 26th, 2007, 11:06 AM
#4
Thread Starter
Hyperactive Member
Re: Fill textboxes by clicking on DGV using BindingSource
jmcilhinney how do I make those text boxes change dynamically as I click on the grid? As it is now it complains after I choose a second row.
---------------------------
Data Error
---------------------------
This causes two bindings in the collection to bind to the same property.
Parameter name: binding
System.Windows.Forms
---------------------------
OK
---------------------------
-
Sep 26th, 2007, 06:14 PM
#5
Re: Fill textboxes by clicking on DGV using BindingSource
Like I said, you bind the TextBoxes to the same BindingSource, e.g.
vb.net Code:
myBindingSource.DataSource = myDataTable
myDataGridView.DataSource = myBindingSource
myTextBox.DataBindings.Add("Text", myBindingSource, "SomeColumn")
When you select a record in the grid the Current property of the BindingSource is updated, which will in turn update the bound TextBox, as well as any other controls bound to the same source. It's all automatic once the bindings are set.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|