How to copy One row to another row in gridview - vb.net

Using VB.Net
I want to Copy one row data to another row.
I am using checkbox in the gridview, if i clicked the checkbox and press button then selected row copy to new cell (row)
Below code is working for delete, not working for copy the rows
Code
Private Sub btncopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
If m_row.Cells("chksel").Value = True Then
Me.grvList.Rows.Add(m_row)
' Me.grvList.Rows.Remove(m_row)
End If
Next
End Sub
The above code is showing error as "Row provided already belongs to a DataGridView control."
What wrong with my code.
Need VB.Net Code Help

You can't add exactly the same row again. You will need to create a new row and populate it with the values from the row you are duplicating instead, then add the new row to the grvList.Rows
I am not sure what sorts of values you have in each cell, but as long as they are value types something like the following should work:
For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
If m_row.Cells("chksel").Value = True Then
'Create new row to hold duplicated values
Dim NewRow As DataRow = grvList.NewRow()
'loop thru existing rows to copy values
For i As Integer = 0 To m_row.Cells.Count - 1
NewRow(i) = m_row.Cells(i).Value
Next
'Add newly create row to table
Me.grvList.Rows.Add(NewRow)
' Me.grvList.Rows.Remove(m_row)
End If
Next
Keep in mind that if the items in any of the cells are reference types that you will still be referencing the same item, instead of creating a copy of the item. Much as you were doing by simply calling add on the same row you located.
Sorry, I missed that the rows were DataGridView rows, rather than a datatable that was bound... this should do the trick in that case:
For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
If m_row.Cells("chksel").Value = True Then
'Create new row to hold duplicated values
Dim NewRow As DataGridViewRow = m_row.Clone
'Add newly create row to table
Me.grvLIst.Rows.Add(NewRow)
End If
Next

'To copy Row
Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
CopyRowIndex = DGV1.CurrentRow.Index
End Sub
'To Paste Row
Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
PasteRowIndex = DGV1.CurrentRow.Index
For index As Int32 = 0 To DGV1.ColumnCount - 1
DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
Next
End Sub
'To Duplicate Rows
Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
CopyRowIndex = DGV1.CurrentRow.Index
DGV1.Rows.Add()
DuplicateRowIndex = DGV1.Rows.Count - 1
For index As Int32 = 0 To DGV1.ColumnCount - 1
DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
Next
End Sub

Related

filling datagridview from an array

How can I fill a datagridview from an array or a list.
I have the following line that get the like in a datagridview from a textbox and adds it to an array, and I want to fill a datagridview with these values. how can I do that.
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
Dim match As DataGridViewCell() = (From row As DataGridViewRow In Me.DataGridView2.Rows From cell As DataGridViewCell In row.Cells Select cell Where CStr(cell.FormattedValue).Contains(Me.TextBox4.Text)).ToArray()
DataGridView2.DataSource = match.ToList()
end sub
tried this and it didnt work...
As I understand, here you me humble example to do that. But remember that we have an array which consists of five element "Arr(5)" with some values, then add these values in a specific DGV column, so please do the appropriate modification for your exact need.
Here is the code:
Public Class Form1
Private Sub BtnFillDGVFromArray_Click(sender As Object, e As EventArgs) Handles BtnFillDGVFromArray.Click
Dim Arr(5) As String
' This to give the array initial values to aviod the null reference
For i = 0 To 4
Arr(i) = Nothing
Next
' Add some strings to the array
For i = 0 To 4
Arr(i) = "ArrayVar(" & i & ")"
Next
DGV.Columns.Add("Col1", "Col1")
For i = 0 To UBound(Arr) - 1
DGV.Rows.Add(Arr(i).ToString)
Next
End Sub
End Class

How to pass the 2nd selected row in datagridview to textbox?

How do i pass my 2nd selected row in datagridview to textboxt. I only know how to put the first data. How do i pass the 2nd selected to textbox?
Dim i As Integer
i = DataGridView2.SelectedRows(0).Index
Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value
you can use this code it worked for me
Dim test As String = DataGridView1.SelectedRows(0).Cells(2).Value.ToString
You need to only change the .cell(Here write the index value of the cell)
Then you can use the string value to fill up textbox
Try using the following:
Dim secondRow as integer = 0
Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
'assuming that by second selected row, you mean the row after the selected row
secondRow = e.RowIndex + 1
End Sub
Private Sub RowToTextBox()
Try
For i = 0 to DataGridView2.ColumnCount - 1
txtEmployeeID.Text &= DataGridView2.Item(i, secondRow)
Next
Catch ex as Exception
MsgBox("You have selected the final row")
End Try
End Sub
I don't think Me. is needed when referring to a textbox in the same form.
Since you don't know how many rows your user will select, I think looping thought the SelectedRows collection might work. I used the Name column because that is what I happened to have in my grid. Instead of a MessageBox you could add the values to a ListBox.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each GridRow As DataGridViewRow In DataGridView1.SelectedRows
MessageBox.Show($"Value is {GridRow.Cells("Name").Value}")
Next
End Sub

Silverlight Datagrid New Row Goes to Bottom and Screws Up Multi-Delete Logic

I have a Silverlight 4 application I've been tasked with making some changes to. I'd rather redo it in something supported, but that's not an option at the moment. I have a datagrid and I've added a button to insert a new row into the datagrid and database table using the values from two comboboxes:
Private Sub btnAddAccountGroupLink_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnAddAccountGroupLink.Click
Dim sv_ag As Com_AccountGroup = cbACCT_GROUP.SelectedValue
Dim sv_cf As CS_FUND = cbACCT_CD.SelectedValue
AccountGroupLinkData.DataView.Add(New Com_AccountGroupLink() With {.ACCT_GROUP = sv_ag.ACCT_GROUP, .ACCT_CD = sv_cf.ACCT_CD})
AccountGroupLinkData.SubmitChanges()
End Sub
When the row is added, it gets added to the bottom of the datagrid instead of it's properly sorted location. This also breaks my code that deletes selected indexes from the datagrid:
Private Sub btnRemoveSelected_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnRemoveSelected.Click
If MessageBox.Show("Are you sure you want to delete the selected records?", "Confirmation", MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
Dim indexesToDelete As New List(Of Integer)
For i As Integer = 0 To AccountGroupLinkData.DataView.Count - 1
If AccountGroupLinkData.DataView(i).IsSelected Then
indexesToDelete.Add(i)
End If
Next i
For d As Integer = 0 To indexesToDelete.Count - 1
AccountGroupLinkData.DataView.RemoveAt(0)
Next d
AccountGroupLinkData.SubmitChanges()
End If
End Sub
So if I delete the bottom row, it actually deletes the row that's located where this row should be.
How can I force the datagrid to update or show the row in its correct position?
I solved this by adding this code after the SubmitChanges:
NavigationService.Refresh()
This refreshes the Silverlight page and the row pops up to the proper position.

Hide rows if column contains particular text/ string

I have a datagridview wich I have no bound datasource too I also have a button and three rows in my datagridview. If my column named STATUS contains the word CLOSED I would like to hide that entire row but i dont want to delete it just hide it.
If anyone woyuld like to know I am ussing VB.net
How can I do this?
If you are using a bound datasource you want to capture the DataGridView.DataSourceChanged event.
Would look like this.
Private Sub DataGridView1_DataSourceChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.DataSourceChanged
For Each row As DataGridViewRow In DirectCast(sender, DataGridView).Rows
If row.Cells("status").Value.ToString.ToLower.Contains("Closed") Then
row.Visible = False
End If
Next
End Sub
If you are not using a datasource you would want to capture the DataGridView.RowsAdded event.
Would look like this.
Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Dim dg As DataGridView = sender
If dg.Columns.Count > 0 And e.RowIndex <> 0 Then
Dim theRow As DataGridViewRow = dg.Rows(e.RowIndex)
If theRow.Cells("status").Value.ToString.ToLower.Contains("closed") Then
theRow.Visible = False
End If
End If
End Sub

get value of selected item in second column... returning previous item

I get that there are other questions like this, but for some reason nobody seems to have this problem. I'm trying to populate a 2 column listview that I create on load like this.
With lstShipMethods
.View = View.Details
.FullRowSelect = True
.HeaderStyle = ColumnHeaderStyle.None ' set to whatever you need
.Columns.Clear() ' make sure collumnscollection is empty
' Add 3 columns
.Columns.AddRange(New ColumnHeader() {New ColumnHeader(), New ColumnHeader()})
End With
lstShipMethods.Items.Add(New ListViewItem({"col1data", "col2data1"}))
lstShipMethods.Items.Add(New ListViewItem({"col1data", "col2data2"}))
It populates just fine, but when I try to get the data from the selected items column like this
Private Sub lstShipMethods_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstShipMethods.SelectedIndexChanged
Dim val As String = lstShipMethods.FocusedItem.SubItems(1).Text
MessageBox.Show(val)
End Sub
after the first click it will always show both values aka second column from row one and row two will be output in the MessageBox.
You can use the ItemSelectionChanged event instead.
Private Sub lstShipMethods_SelectedIndexChanged(sender As System.Object, e As ListViewItemSelectionChangedEventArgs) Handles lstShipMethods.ItemSelectionChanged
If e.IsSelected Then
Dim val As String = lstShipMethods.FocusedItem.SubItems(1).Text
MessageBox.Show(val)
End If
End Sub