Dear Experts

I use following codes to select data in ListView1, given value in textbox1.text. That codes work fine.

Textbox1 data is integer and DataGridView1 first Column Value is Integer. I want to locate this value in Grid.

My question is how to select data in DataGridVeiw, given data in textbox1.

Please help

Dim itmX As ListViewItem = ListView1.FindItemWithText(TextBox1.Text, False, 0)
        If Not itmX Is Nothing Then
                     itmX.Selected = True
            ListView1.Items(itmX.Index).Selected = True
            itmX.EnsureVisible()
End If

Recommended Answers

All 4 Replies

The following should work for you. I would recommend using the column name if it is a dynamic column otherwise I would use the design time created column index location, rather than saying "Search only column 1". I say this because the user may re-order the columns and column 1 could become column 3.

Dim IntegerToFind As Integer = CInt(ListBox1.SelectedItem.ToString)

            For Each dgvRow As DataGridViewRow In DataGridView1.Rows
                If CInt(dgvRow.Cells("ColumnNameToBeSearched").Value) = IntegerToFind Then
                    'Do what you want to after the value has been located
                End If
            Next

HTH

The following should work for you. I would recommend using the column name if it is a dynamic column otherwise I would use the design time created column index location, rather than saying "Search only column 1". I say this because the user may re-order the columns and column 1 could become column 3.

Dim IntegerToFind As Integer = CInt(ListBox1.SelectedItem.ToString)

            For Each dgvRow As DataGridViewRow In DataGridView1.Rows
                If CInt(dgvRow.Cells("ColumnNameToBeSearched").Value) = IntegerToFind Then
                    'Do what you want to after the value has been located
                End If
            Next

HTH

Dear Sir,

DataGridView has data as follows

sno---name
1-----a
2-----b
3-----c

I need codes for to search value given in textbox, suppose textbox has 2
then datagridview must display 2----b column.

You provided me code to search index number given in textbox.

Please help again

Change the integer to find and the ColumnNameToBeSearched and you should be able find the row you're looking for.

I'm confused when you say "suppose textbox has 2
then datagridview must display 2----b column." Do you want only that row to be visible?
If that's the case use the if then statement else condition to set the datagridviewrow to false, as follows:

For Each dgvRow As DataGridViewRow In DataGridView1.Rows
                If CInt(dgvRow.Cells("ColumnNameToBeSearched").Value) = IntegerToFind Then
                    'Do what you want to after the value has been located
                    dgvRow.Visible = True
                Else
                    dgvRow.Visible = False
                End If
            Next

Change the integer to find and the ColumnNameToBeSearched and you should be able find the row you're looking for.

I'm confused when you say "suppose textbox has 2
then datagridview must display 2----b column." Do you want only that row to be visible?
If that's the case use the if then statement else condition to set the datagridviewrow to false, as follows:

For Each dgvRow As DataGridViewRow In DataGridView1.Rows
                If CInt(dgvRow.Cells("ColumnNameToBeSearched").Value) = IntegerToFind Then
                    'Do what you want to after the value has been located
                    dgvRow.Visible = True
                Else
                    dgvRow.Visible = False
                End If
            Next

Dear Sir,

I modified your codes as

For Each row As DataGridViewRow In DataGridView1.SelectedRows
            row.Selected = False
        Next

        For Each DataGridViewRow As DataGridViewRow In DataGridView1.Rows
            If CInt(DataGridViewRow.Cells(0).Value) = Me.TextBox1.Text Then
                DataGridViewRow.Selected = True
            End If
        Next

Now everything is working fine,
Thanks for helping

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.