How to copy rows between a DataGridView bound with a DataTable - vb.net

I would like to know how can I copy a row in another DataGridView. They are bound with a datatable. I'm trying this but it don't works!
For k As Integer = 0 To gridA.SelectedRows.Count
Dim row As DataRow = dtA.Rows(gridA.SelectedRows(k).Index)
dtB.ImportRow(row)
Next
Then delete rows from gridA:
For k As Integer = 0 To gridA.SelectedRows.Count
dtA.Rows.RemoveAt(gridA.SelectedRows(k).Index)
Next

Using the index of the data grid can be a problem since the rows might be sorted in a different order than the DataTable.
I re-worked your example to use the actual row objects to move and delete:
Dim rows As DataGridViewSelectedRowCollection = gridA.SelectedRows
For Each row As DataGridViewRow In rows
dtB.ImportRow(DirectCast(row.DataBoundItem, DataRowView).Row)
Next
For Each row As DataGridViewRow In rows
dtA.Rows.Remove(DirectCast(row.DataBoundItem, DataRowView).Row)
Next

Related

"This row already belongs to this table" even with new row inside loop

I've read previous posts regarding this but there is something else happening that I'm unaware of. All I'm trying to do is insert a new blank row at row 1 and 3. Also I'm pretty sure the row index numbers are wrong since this is going backwards.
Dim dsTemp As System.Data.DataSet
Dim dtTemp As System.Data.DataTable
Dim sheet As OfficeOpenXml.ExcelWorksheet = Nothing
dsTemp = CType(Me.uwgGeneratedMatrix.DataSource, System.Data.DataSet)
dtTemp = dsTemp.Tables(0)
If dsTemp.Tables(0).Columns.Contains("VerticalID") Then
dsTemp.Tables(0).Columns.Remove("VerticalID")
End If
If dsTemp.Tables(0) IsNot Nothing Then
For index As Integer = dsTemp.Tables(0).Rows.Count - 1 To 0 Step -1
Dim dsRow As System.Data.DataRow = dsTemp.Tables(0).Rows(index)
Dim value As Object = dsRow.Item(0)
If Not Microsoft.VisualBasic.IsDBNull(value) AndAlso String.Compare(value.ToString, "ColDes", True) = 0 Then
Dim newRow As System.Data.DataRow = dsTemp.Tables(0).NewRow
dsTemp.Tables(0).Rows.Remove(dsRow)
dsTemp.Tables(0).Rows.InsertAt(dsRow, 0)
dsTemp.Tables(0).NewRow()
dsTemp.Tables(0).Rows.InsertAt(dsRow, 2)
End If
Next
End If
Look at your code:
Dim dsRow As System.Data.DataRow = dsTemp.Tables(0).Rows(index)
'...
If Not Microsoft.VisualBasic.IsDBNull(value) AndAlso String.Compare(value.ToString, "ColDes", True) = 0 Then
Dim newRow As System.Data.DataRow = dsTemp.Tables(0).NewRow
dsTemp.Tables(0).Rows.Remove(dsRow)
dsTemp.Tables(0).Rows.InsertAt(dsRow, 1)
dsTemp.Tables(0).NewRow()
dsTemp.Tables(0).Rows.InsertAt(dsRow, 3)
End If
What is that code doing? Look at each line and actually ask yourself what it does.
First you get an existing row form the table and assign it to the
dsRow variable.
Inside the If block, you create a new row and assign it the
newRow variable. You never use that variable again, so what was the point of that?
You then remove the existing row from the table.
You then insert the existing row as the second row in the table.
You then create a new row and do nothing with it at all, so what was the point of that?
You then try to insert the existing row a second time, as the fourth row in the table.
Is it really unexpected that the error message is telling you that that row already belongs to the table?
For one thing, when you call NewRow, that simply creates a new row. That row is NOT added to the table automatically. How could it be? It's empty and the table may have non-nullable columns. You are supposed to populate it as required and then add it yourself.
Apart from that, you can't add or insert the same row twice.
If all you're trying to do is insert blank rows then you need to actually insert blank rows, not existing rows that you removed. You need to twice call NewRow followed by Insert, e.g.
myTable.Rows.Insert(myTable.NewRow(), 1)
myTable.Rows.Insert(myTable.NewRow(), 3)
I'm dumb, apparently you have to do dsTemp.Tables(0).Rows.InsertAt(newRow, 2)

Load a DataGridView with 100 empty rows with Row number at loading in vb.net

I want to show/load 100 empty rows with row number in datagridview of vb.net at run time, so that user can enter the data. If user fill all the 100 rows then datagridview can create new rows as it do usually. I found this so many time but not find any solution how to load empty rows at run time.
'dgv is your DataGridView object.
dgv.Rows.Add(100)
Dim I As Integer = 1
For Each Row As DataGridViewRow In dgv.Rows
Row.Cells(0).Value = I.ToString()
I += 1
Next

Deleting and adding items in a table Excel VBA

I've got a problem with filling a table.
The table will be filled with data after a combobox is changed. First off I want to delete the data that's in the table and after that add the data I want.
What I have got is this (This is just a part of the code, the collection is already filled with data)
Dim TableListObject As ListObject
Dim TableObjectRow As ListRow
Dim i As Integer
Dim CollWerknemer as Collection
Set TableListObject = TheSheet.ListObjects(1)
Set TableObjectRow = TableListObject.ListRows.Add
TableListObject.DataBodyRange.Delete
i = 1
For Each vNum In CollWerknemer
TableObjectRow.Range(i, 1) = vNum
i = i + 1
Next vNum
The problem is that it does delete the table but doesn't add anything.
If I exclude the TableListObject.DataBodyRange.Delete from the code it does fill the table with the data that I want, but if I change my combobox the new data will be added at the bottom instead of clearing the table first.
First you could iterate each row in the current table, and remove its content, then use the ListRows.Add
With outTBL
If .ListRows.Count >= 1 Then
.DataBodyRange.Delete
End If
End With
Dim row as ListRow
Set row = outTBL.ListRows.Add
row.Range(x, x) = vNum

navigating through records on a database

Simplest way how to loop and display through records using textbox, combo box and datetimepicker.
Below is a code but still cant figure it out
Dim dr As DataRow
Dim ds As Dataset
Dim dt As DataTable
<code to fill the dataset>
dt = ds.Tables(0)
For Each dr In dt.Rows
Console.WriteLine (dr("ColName"))
Next
ds.Dispose()
You need two loops to do this - an outer loop for the number of rows in the table, and an inner loop for the columns in each row.
You have the For Each for the rows already, but you need to know how many columns are in the DataRow, and print out the value in each column. You can get the count of columns from the DataTable.
Dim cols As Integer
cols = dt.Columns.Count - 1
For Each dr in dt.Rows
For i As Integer = 0 To cols
Console.WriteLine(dr(i).ToString())
Next
Next
Notice that I call ToString() on the value returned from each column, and reference the column by it's ordinal. When you access a specific column in the DataRow, it returns an Object, so you'll need to cast that value to the correct data type for use in your program.
The code you posted would have only printed the value for the column that had "ColName" as it's column name for each row. If you didn't have a column named "ColName" then you would see an error.

How can I update a row in a DataTable in VB.NET?

I have the following code:
Dim i As Integer = dtResult.Rows.Count
For i = 0 To dtResult.Rows.Count Step 1
strVerse = blHelper.Highlight(dtResult.Rows(i).ToString, s)
' syntax error here
dtResult.Rows(i) = strVerse
Next
I want to add a strVerse to the current row.
What am I doing wrong?
The problem you're running into is that you're trying to replace an entire row object. That is not allowed by the DataTable API. Instead you have to update the values in the columns of a row object. Or add a new row to the collection.
To update the column of a particular row you can access it by name or index. For instance you could write the following code to update the column "Foo" to be the value strVerse
dtResult.Rows(i)("Foo") = strVerse
You can access columns by index, by name and some other ways:
dtResult.Rows(i)("columnName") = strVerse
You should probably make sure your DataTable has some columns first...
Dim myRow() As Data.DataRow
myRow = dt.Select("MyColumnName = 'SomeColumnTitle'")
myRow(0)("SomeOtherColumnTitle") = strValue
Code above instantiates a DataRow. Where "dt" is a DataTable, you get a row by selecting any column (I know, sounds backwards). Then you can then set the value of whatever row you want (I chose the first row, or "myRow(0)"), for whatever column you want.