DataGridView Select Row with Specific Cell value [VB.NET] - vb.net

I am using this code to move the row selection programmatically
For Each row In DataGridView1.Rows()
If row.Cells(0).Value.ToString().Equals(code) Then
row.Selected = True
End If
Exit For
Next
But the Problem I am facing is This only Changes the highlighted row but doesn't actually select the row..
When I try to get data from selected row it gives data from previous row not the blue highlighted row.
Actually Row header Tick type thing doesn't move It stays on previous row.

You also have to set the selceted cell:
For Each row As DataGridViewRow In DataGridView1.Rows()
If row.Cells(0).Value.ToString().Equals(code) Then
row.Selected = True
DataGridView1.CurrentCell = row.Cells(0)
Exit For
End If
Next
The reason your code works only for the first row is that Exit For was outside the If statement. If you include it the code works like a charm.

Related

Compare DataTable and DataGridView and hightlight matching rows in DataGridView

I have a database table which contains a list of usernames and their current active location, an integer called SEQUENCE.
I get this list of sequence numbers into a datatable with a single column (e.g. "SELECT SEQUENCE FROM TABLE"):
dtUsers = CLS_USERS.GetUsers(User)
It excludes the current user, which is why I parse in User.
What I then do is loop through the datatable and for each number I want to set the matching row in a datagridview (which also has a SEQUENCE column) to a different colour. This is my current code:
For Each row As DataRow In dtUsers.Rows
intSeq = row("SEQUENCE")
For Each dgv_row As DataGridViewRow In dgvCandList.Rows
If dgv_row.Cells("CURRENT_SQ").Value = intSeq Then
dgv_row.DefaultCellStyle.BackColor = Color.Cyan
Else
dgv_row.DefaultCellStyle.BackColor = Color.Grey
End If
Next
Next
However, all rows are highlighted rather than just those where dgv_row.Cells("SV_CURRENT_CAND_SQ").Value = intSeq is True... I've verified that this loop is working correctly, so it must be dgv_row.DefaultCellStyle.BackColor = Color.Cyan which is incorrect?
If so, how should I correctly be assigning the row back colour of specific DataGridRow?
EDIT: This code works correctly, my issue was related to bug outside this loop causing the BackColor to be sent for everything, and then never setback to default if where BackColor wasn't being set back to the default if it wasn't in dtUsers.

DataGridView Copy a selected row issue

I have a datagridview and i want to copy the exact row that gets selected by the user to the same position (above). I have it right now so it inserts the line above the current line that is selected but it is not copying all the values.
I'm just using a basic grid, no dataset or datatables. heres my code.
If dgvPcPrevMonth.SelectedRows.Count > 0 Then
dgvPcPrevMonth.Rows.Insert(dgvPcPrevMonth.CurrentCell.RowIndex, dgvPcPrevMonth.SelectedRows(0).Clone)
Else
MessageBox.Show("Select a row before you copy")
End If
The clone method only clones the row, not the data in it. You'll have to loop through the columns and add the values yourself after inserting it
Public Function CloneWithValues(ByVal row As DataGridViewRow) _
As DataGridViewRow
CloneWithValues = CType(row.Clone(), DataGridViewRow)
For index As Int32 = 0 To row.Cells.Count - 1
CloneWithValues.Cells(index).Value = row.Cells(index).Value
Next
End Function
The code is taken directly from the msdn link provided below
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
I updated your code to test if current row was not the first and to clone the cuurent row (not the first one).
If ddgvPcPrevMonth.CurrentCell.RowIndex > 0 Then
dgvPcPrevMonth.Rows.Insert(dgvPcPrevMonth.CurrentCell.RowIndex, dgvPcPrevMonth.Rows(dgvPcPrevMonth.CurrentCell.RowIndex-1).Clone)
Else
MessageBox.Show("Select a row (but not the first one) before you copy")
End If

Move Index to last row in Datagridview

I want to programmatically delete the last row in my Datagridview when a user clicks a button. As of now, the program will scroll down and highlight the last row, but only deletes the row where the index is at. So, I need to move the indexer (triangle indicator) to the last row. How do I do this??
If LftMtr_Data_Grid.RowCount >= 2 Then
LftMtr_Data_Grid.FirstDisplayedScrollingRowIndex = LftMtr_Data_Grid.RowCount - 2 'scroll to the last row
LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 2).Selected = True 'select the last row
LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.CurrentRow.Selected) 'delete selected row
End If
Thank you
The CurrentRow is the row containing the active cell. By setting the Selected property to true, the row is only highlighted but not active.
To make the last row as active i.e. to make the row as CurrentRow use the CurrentCell property. Set any of the cell in the last row as CurrentCell before removing the row.
LftMtr_Data_Grid.CurrentCell = LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 1).Cells(0);
LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.CurrentRow.Index);

Get value of one columns in DataGridView with multi select vb.net

I want get value of one columns in DataGridView with multi select in vb.net
when I use
For Each cell In DGNo.SelectedCells
If cell Is Nothing Then
Exit Sub
End If
If Not FirstValue Then
str += ","
End If
str += cell.Value.ToString
FirstValue = False
Next
it take all values DataGridView. How get value of column(0)?
You can get the individual cell values using the DataGridView.Item property which provides an indexer to get or set cell value in the given location.
DataGridView(ColumnNumber,RowNumber)
In your case, use it like below to get cell value from first column in first row.
DGNo(0,0).Value
To get the value of only column 0 in the selected cells,
For Each cell In DGNo.SelectedCells
If cell.ColumnIndex =0 Then
' do work
End If
Next

For Loop to count checked items on datagrid not counting correctly

I worked out the following loop to count and display how many rows on my datagrid are checked. However, the loop is ignoring my first checked row. The count does not start at 1 until I have checked the second row. The same happens when I uncheck, the values are off by one.
Dim chkRowCount As Integer = 0
For Each row As DataGridViewRow In dgvAssignGridView.Rows
If row.Cells(6).Value = True Then
chkRowCount += 1
Else
chkRowCount += 0
End If
Next
lblChkCount.Text = chkRowCount.ToString
I have tried setting the variable to 1 instead of 0, but that had some unwanted results.
I am guessing you have this code in CellContentClick. The problem is that the code in that routine fires before the value of the checkbox is actually changed. However, you can basically force the DataGridView to verify itself first by putting the following line right before your code.
dgvAssignGridView.EndEdit()
That forces the cell click to register before you do your count.