VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hallo,
I have an access database bound to a data grid view using a binding source. I want to make a copy of a current row in the data grid view or binding source with a button click. I tried following but it gives me a row that already belongs to the data table
Dim rowView As DataRowView = DirectCast(BS.Current, DataRowView)
Dim nRow As DataRowView = DirectCast(BS.AddNew(), DataRowView)
nRow.BeginEdit()
nRow.Row.BeginEdit()
nRow.DataView.Table.Rows.Add(rowView)
BS.MoveLast()
Datatable has a auto field primary column
Thanks
Hi
Give this a try and see if it helps.
Dim rowView() As Object = DirectCast(BS.Current, DataRowView).Row.ItemArray
Dim nRow As DataRow = dt.NewRow
nRow.ItemArray = rowView
' here I have used InsertAt but
' just .Add would work too
dt.Rows.InsertAt(nRow, BS.Position)
Hi
Try this variation (whole test project posted but see changes in last sub)
' Form1 with DataGridView1,
' RadioButtons 1 and 2
' Label1 and Button1
Option Strict On
Option Explicit On
Public Class Form1
Dim dt As New DataTable("Freddy")
Dim BS As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With dt
.Columns.Add("ID", GetType(Integer))
.PrimaryKey = New DataColumn() { .Columns("ID")}
With .Columns("ID")
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
End With
.Columns.Add("Catagory", GetType(Double))
.Columns.Add("Product", GetType(Double))
End With
' few sample data values
dt.Rows.Add(Nothing, 12.34, 21.32)
dt.Rows.Add(Nothing, 1.3, 1.2)
dt.Rows.Add(Nothing, 2.4, 2.3)
BS.DataSource = dt
DataGridView1.DataSource = BS
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
Dim rb As RadioButton = DirectCast(sender, RadioButton)
If rb.Checked Then
Label1.Text = CDbl(dt.Compute("SUM(Product)", String.Empty)).ToString("C2")
Else
Label1.Text = 0.ToString("C2")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rowView() As Object = DirectCast(BS.Current, DataRowView).Row.ItemArray
' contrained column
rowView(0) = Nothing
Dim nRow As DataRow = dt.NewRow
nRow.ItemArray = rowView
' .Add row to bottom
dt.Rows.Add(nRow)
End Sub
End Class