DataGridView retrieve checked row - vb.net

The thing is that I have DataGridView1, when I click on it I store some values and after clicking on *edit* button new window opens and I retrieve all info from that selected row cells.
But, when I turn off that window I set the code to automatically refresh my DGV1, what I need is after refreshing to retrieve that previously checked row and get it again so my client can easily continue working since they have like 10.000 rows so they don't want to search for next row everytime when they edit something.

I don't know how you are retrieving the current Row/Column indices, but I am writing a code triggered from the CellClick Event which you shouldn't find any problem to adapt to your specific requirements:
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim selectedRow As Integer = e.RowIndex
Dim selectedCol As Integer = e.ColumnIndex
fillDataGridView()
DataGridView1.FirstDisplayedCell = DataGridView1(selectedCol, selectedRow)
End Sub
Where fillDataGridView() performs the refilling.

Related

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.

Need to click multiple times to get a cell value datagrid issue

I am trying to click on cell to get its value. Some time it works, some times i have to click multiple times to get the value.
What's Wrong with my code?
on Form Load
SQL = Select Id, Cat_name From cat_tbl
Private Sub dgcat_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgcat.CellContentClick
If dgcat.Rows(e.RowIndex).Cells(e.ColumnIndex).Value IsNot Nothing Then
ids = (dgcat.Rows(e.RowIndex).Cells(0).Value.ToString())
End If
End Sub
On Delete
SQL = Delete from cat_tbl where ID=ids
The problem is that you are using the DataGridView.CellContentClick Event which triggers only if your mouse cursor is placed over the content and does not trigger if your mouse cursor is over the cell but not over the content. Examples:
You do not have a hit because you are not clicking the content:
You have a hit because you are clicking the content:
That is why it looks like sometimes works, sometimes not. One solution would be to use the DataGridView.CellClick Event instead of the CellContentClick:
Private Sub DataGridView1_CellClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If (e.ColumnIndex <> -1) Then
Dim value = DataGridView1.Rows(e.RowIndex).Cells(0).Value
If (Not value Is Nothing) Then
Dim ids = value.ToString()
MessageBox.Show(ids)
End If
End If
End Sub
This works always:
In your case the problem is very mean, because you are hitting sometimes exactly the area of couple of pixels that does not respond because it does not count as content:
Lemon,
Please understand that cell content click is just that. Cell Content. Clicking only on the row will not provide you with the cells content, or value as you are looking for.
Please, notify that you are indeed clicking on the cell content.
In testing, I provided this code. If you do not receive the cells content when clicking on the cell's content, we may need to explore other options.
Dim ids As String
If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value IsNot Nothing Then
ids = (DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString())
End If
MsgBox(ids)

Get value from DataGridView on form load

I am trying to get a value fro a DataGridView 'DGV' when my winform loads ie, Forms2_load. DGV is bound and I am using the code below to Fill the DGV.
My question is, how do I get a value from 'DGV' without any user intervention, just when the form loads. I tried a for next loop on the form load event, but no message pops up. Can someone please help with this. Thanks
Me.CustomersTableAdapter.Fill(Me.StorageDataSet1.Customers)
Dim values As String
For Each RW As DataGridViewRow In DGV.SelectedRows
'Send the first cell value into messagebox'
values = RW.Cells(0).Value.ToString
Next
MessageBox.Show(values)
You might get better results if you use the bindingsource that the DGV is connected to. It will get a CurrentChanged event only after its loaded or when the position moves.
Private Sub BindingSource(sender As System.Object, e As System.EventArgs) Handles BindingSource.CurrentChanged
Dim oVw As DataRowView
oVw = TryCast(BindingSource.Current, DataRowView)
If Not oVw Is Nothing Then
MessageBox.Show(oVw.Item(0).ToString)
'
End If
End Sub
This will trigger each time the DGV moves to a new position. Change the name to whatever your BindingSource is.

Interacting with a Data Grid View in Visual Basic

I have a Data Grid View pulling in rows of data from an Access Database. I would like to code it so that when a user double clicks on a row, a window opens with that particular row being opened.
In addition, I would like to code a button so that it does the same, that is open a window with the selected row data.
How can I get the selected row ID or other data in VB?
EDIT: Forgot to mention this is a DataGridView.
DataGridView?
Private Sub dgvSource_CellContentDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSource.CellContentDoubleClick
Dim intRow As Integer = e.RowIndex
Dim dgvr As DataGridViewRow = dgvSource.Rows(intRow)
Dim drw As DataRow = DirectCast(dgvr.DataBoundItem, DataRowView).Row
MsgBox(drw("ID"))
End Sub
"ID" is the name of the column you want. For a button, set intRow based on selection.

how to put search functionality in dataGridView in vb.NET

How can I select a single cell from selected row in datagridView and after selecting that I want to put simple search functionality (like we have in our windows folders-typing any characters and search should work)?
I do not really understand your question. If you want to select one cell, you can use the celldoubleclick event for exemple. And the to get the selected cell, use e.rowindex and e.columnindex which will give you the row and the column where the cell is located.
You can try this as a possible solution.
Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = m_CustomersBindingSource
m_CustomersBindingSource.DataSource = nwData.Customers
Then you can sort using the BindingSource.
CustomersBindingSource.Sort = "ContactName ASC"
And you can find using the BindingSource.
Dim index as integer = _
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
If index <-1 then 'it was found; move to that position
CustomersBindingSource.Position = index
End If
You could then populate:
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
with the keys pressed in the cell by capturing them by utilizing:
Private Sub DataGridView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
Dim dgv As DataGridView = TryCast(sender, DataGridView)
If dgv IsNot Nothing Then
'You will need some logic here to determine how long to wait between keyups
'Perhaps a timer that ticks every500 milliseconds and reset on keyup.
e.KeyData
End If
End Sub
I found the original Biding Source Logic at : This Location