how to put search functionality in dataGridView in vb.NET - 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

Related

datagridview values remove from another datagridview

I have two datagridviews. One is dtgridPopulate and is populated with data from the database.
dtgridPopulate columns are (checkbox, code, name)
checkbox
code
name
checkbox icon
c1
customer_one
Then the second datagridview is dtgridGenerate which has generated values from dtgridPopulate.
I use the code dtgridPopulate.Rows.Add(code, name) to add manually the value from dtgridPopulate to dtgridGenerate.
dtgridGenerate columns are (code, name)
code
name
c1
customer_one
When I check the checkbox in the dtgridPopulate it will transfer the values (code, name) to the dtgridGenerate. But the problem is when I uncheck the checkbox in the dtgridPopulate, It should also REMOVE the values in the dtgridGenerate.
Private Sub dtgridPopulateSelectAll_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dtgridPopulate.CurrentCellDirtyStateChanged
RemoveHandler dtgridPopulate.CurrentCellDirtyStateChanged, AddressOf dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
If TypeOf dtgridPopulate.CurrentCell Is DataGridViewCheckBoxCell Then
dtgridPopulate.EndEdit()
Dim Checked As Boolean = CType(dtgridPopulate.CurrentCell.Value, Boolean)
If Checked Then
code = dtgridPopulate.CurrentRow.Cells(1).Value.ToString
name = dtgridPopulate.CurrentRow.Cells(2).Value.ToString
dtgridGenerate.Rows.Add(code, name)
Else
For Each drow As DataGridViewRow In dtgridPopulate.SelectedRows 'This is for uncheck but it doens't work
dtgridGenerate.Rows.Remove(row)
Next
End If
End If
AddHandler dtgridPopulate.CurrentCellDirtyStateChanged, AddressOf dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
End Sub
I refer to this reference
Error when I uncheck the checkbox: row provided does not belong to this datagridview control. parameter name: datagridviewrow
I am not sure “why” you have unsubscribed and then re-subscribed to the event. Typically, this is only needed if the code in the event “changes” something in the grid that would cause the event to re-fire and this is not happening in your current code. Nor is ending the grids edit necessary.
Also, your use of the grids CurrentCellDirtyStateChanged event has one drawback in relation to what you want to do… it will NOT re-fire if the user checks and unchecks the same cell over and over. In other words, if the user clicks a check box cell and the check box becomes checked and the event fires… then … if the user “unchecks” the same cell before clicking on any other cell, then the event will NOT refire.
Also, it looks like the event is using a different grid from the signature of the event… dtgridPopulateSelectAll_CurrentCellDirtyStateChanged … ? … “SelectAll” and the grid in the event is called… dtgridPopulate … this is confusing.
Given this, I suggest two things to help. 1) create and use an “Empty” DataTable for the second grid. This way we could simply add and remove items from that table. 2) use the grids CellContnetClick event for this. It will re-fire if the user clicks on the same cell over and over in addition to firing ONLY when the check box is changed.
So, I propose these small changes. First add a global variable named generateDT as a DataTable used as a DataSource to the second grid. Initially this table would be empty. Second use the grids CellContentClick event to make these additions and removal of the rows.
Dim generateDT As DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.Add("checkbox", GetType(Boolean))
dt.Columns.Add("code", GetType(String))
dt.Columns.Add("Name", GetType(String))
dt.Rows.Add(False, "c1", "customer 1")
dt.Rows.Add(False, "c2", "customer 2")
dt.Rows.Add(False, "c3", "customer 3")
dtgridPopulate.DataSource = dt
generateDT = New DataTable()
generateDT.Columns.Add("code", GetType(String))
generateDT.Columns.Add("Name", GetType(String))
dtgridGenerate.DataSource = generateDT
End Sub
Then in the cell content click event, if the clicked cell is a check box cell, then… grab the checked state and both the code and name of the clicked-on row. Then if the check box cell is checked, then simply add the code and name to the second grids global DataTable variable generateDT. If the check box is unchecked, then we will loop through each row in the second grid and if we find a match then simply remove that row from the second grids global DataTable… something like…
Private Sub dtgridPopulate_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dtgridPopulate.CellContentClick
If e.ColumnIndex = 0 Then
Dim Checked = CType(dtgridPopulate.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue, Boolean)
Dim code = dtgridPopulate.CurrentRow.Cells(1).Value.ToString
Dim Name = dtgridPopulate.CurrentRow.Cells(2).Value.ToString
If Checked Then
generateDT.Rows.Add(code, Name)
Else
Dim rowToRemove As DataRowView
For Each row As DataGridViewRow In dtgridGenerate.Rows
If row.Cells(0).Value.ToString() = code Then
rowToRemove = CType(row.DataBoundItem, DataRowView)
generateDT.Rows.Remove(rowToRemove.Row)
Exit For
End If
Next
End If
End If
End Sub

How to update a Text cell using a ComboBox cell VB.NET DatagridView

I am using Friend WithEvents PersonName As DataGridViewComboBoxColumn as a column in DataGridView and PersonName DataSoucre is bounded to a database. I would like for when the ComboBox Selection is changed it uses that value to query my database Business Object to find the right value then I want to set the value from the query to the column to the right of the one with the ComboBox which is named Colum3.
But am not use how to access each individual event handler for the ComboBox cell, I tried dgvTable.CellBeginEdit but it triggers too early.
Private Sub dgvTable_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvTable.CellBeginEdit
If e.RowIndex <> -1 And e.ColumnIndex <> -1 Then
dgvTable.Rows(e.RowIndex).Cells("Colum3").Value = dgvTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value & "Somevalue from my databse"
End If
End Sub

Not allow selection of a certain column in a DataGridView control

I have this datagridview
I was wondering if there is a way to disable the abillity of the user to select cells of the first column (the one with an arrow), but i still need the user to be able to select all other cells,exept the ones on the first column.
If the selection is moved to the target column, using the DataGridView1.SelectionChanged event you can prevent the selection focus, setting the DataGridView1.CurrentCell to next column cell in the same row.
This works for selection events generated by both Mouse clicks and cursor movement.
Private blockedColumn As Integer = 0
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If DataGridView1.CurrentCell.ColumnIndex = blockedColumn Then
DataGridView1.CurrentCell =
DataGridView1(blockedColumn + 1, DataGridView1.CurrentCell.RowIndex)
End If
End Sub
You could set dataGridView1.Columns(0).Frozen = True anyway, for other uses. But it's not necessary.

Prevent Code Running if Combobox's SelectedIndex was Never Changed

I have a combo box that has a couple items in them, where they add text to a text box. I want to prevent the same item being selected twice in a row, because the index never changed, but it still runs the code.
Here's what I've done:
Dim intComboIndex As Integer = -1
Dim cboComboBox As ComboBox = CType(sender, ComboBox)
Dim intComboSelIndex As Integer = cboComboBox.SelectedIndex
If intComboSelIndex > -1 And intComboSelIndex <> intComboIndex Then intComboIndex = intComboSelIndex
Is there a more efficient way of doing this, without having to create a different combo box and compare the indexes?
You must store the previously selected index elsewhere, because at the moment you recreate the variable every time the event is raised.
I'd recommend storing the previously selected index at class level, and then compare that to the currently selected index.
Public Class Form1
Dim ComboBoxPrevIndex As Integer = -1 'Declared at class level, outside any Sub or Function.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim SenderBox As ComboBox = DirectCast(sender, ComboBox) 'Get the control that raised the ever.
'Has the selection changed?
If ComboBoxPrevIndex <> SenderBox.SelectedIndex _
AndAlso SenderBox.SelectedIndex > -1 Then
'Do stuff...
End If
ComboBoxPrevIndex = SenderBox.SelectedIndex 'Set the new, previous index.
End Sub
End Class
You might have noticed that I used AndAlso instead of And. This is because of AndAlso is short-circuited, meaning that it will only check the condition on it's right if the condition on it's left evaluates to True.

DataGridView retrieve checked row

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.