Data grid view colomns search - vb.net

I have a code which will allow me to search every row for a specific text. however I want to modify it so as to be able to search for a specific text in the currently selected column?
The code I'm trying to work with is as follows:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button2.Click
Dim rowindex As String
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("Column1").Value = TextBox2.Text Then
rowindex = row.Index.ToString()
MsgBox(rowindex)
Else
MsgBox("No Record found")
End If
Next
End Sub

You will need to use the SelectionChanged event of the datagrid. The arguments will point to the row that is selected and then you can iterate through the cells in that row and compare values to see if you have a match in the selected row.
If you have a fancy grid that allows more than one row to be selected you will need to loop through each selected row and each cell in those rows.

If you want to search only in selected cells, use the DataGridView.SelectedCells property.
For Each cell As DataGridViewCell In datagridview1.SelectedCells
If cell.Value.ToString() = TextBox2.Text Then
End If
Next
My VB is rusty, just add a null check for cell.Value before calling ToString().

Look for a column instead of looking through Column1 using DataGridView.CurrentCell property:
DataGridView.CurrentCell # MSDN
Code:
Dim currentcolumn As DataGridViewColumn =
DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex)
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item(currentcolumn.Name).Value = TextBox2.Text Then
End If
Next

Related

How to make check all in datagridview?

Hey i'm curently learning vb.net and i want to know how to check in data gridview when all the row will be checked if i have a checkbox outside. And how can i get the data from whe the row checked
To check all you need to setup a loop like this :
For Each Rw As DataGridViewRow In DataGridView1.Rows
DataGridView1.Item(0, Rw.Index).Value = True
'item(Col.index, Row.index) so you can set value on each cell of the datagrid
Next
to uncheck you juste avec to set the line to false
To get a value when a row is check, you need to use events. Here an exemple that give you the value of the second column when you check the first one :
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex = 0 Then
MessageBox.Show("Checkbox changed ! The value of the second column is : " & DataGridView1.Item(1, e.RowIndex).Value)
End If
End Sub

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

dgv add row number to headercell

I'm trying to show the rownumber of a datagridview in the headercell I was under the impression that this was possible but I can't get it to show the value.
What do I need to do?
** The dgv is bound with BindingSource **
My attempt:
Private Sub NumberRows()
For Each oRow As DataGridViewRow In dgvBodyOverview.Rows
oRow.HeaderCell.Value = (oRow.Index + 1).ToString
Next
End Sub
To reproduce your problem, I followed your comment:
I'm calling it from a sub that loads all rows and at the end the call is made.
Doing this, the numbers failed to show whether or not the grid had a DataSource. Instead, I've typically done this as Plutonix suggested above - in DataGridView.CellFormatting.
Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
Dim header As DataGridViewRowHeaderCell = Me.dataGridView1.Rows(e.RowIndex).HeaderCell
If e.ColumnIndex = 0 Then
header.Value = [String].Format("{0}", e.RowIndex + 1)
End If
End Sub
I check for ColumnIndex = 0 to ensure the header cell value is only set once per row. See this C# answer for additional explanation.

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