none
How do I rediplay a datagridview that I searched for a speific item and found the row column of the item

    Question

  • How do I redisplay a datagridview that I searched for a specific item and found the row column of the item

    Tuesday, March 28, 2017 3:22 PM

Answers

  • Hi

    Maybe this veers towards your requirement. It shows a DataGridView filter in use. If you put the selection (cell) in a column then the Button1 click will filter the rows based on the contents of the TextBox1 matching the selected column cell contents.

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' and Button1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            With DataGridView1
                .DataSource = myTable
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            DataGridView1.DataSource = view
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Trim(TextBox1.Text).Length < 1 Or DataGridView1.SelectedCells.Count < 1 Then
                view.RowFilter = Nothing
                Exit Sub
            End If
            Dim c As Integer = DataGridView1.SelectedCells(0).ColumnIndex
            Filter(c)
        End Sub
        Sub Filter(c As Integer)
            Dim h As String = DataGridView1.Columns(c).Name
            Dim v As String = DataGridView1.SelectedCells(0).Value.ToString
            view.RowFilter = h & "='" & TextBox1.Text & "'"
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by BB2017 Friday, April 7, 2017 7:07 PM
    Tuesday, March 28, 2017 6:17 PM
  • Hello,

    The solution will differ depending on how you loaded the DataGridView e.g. using TableAdapter/DataSet/BindingSource or from a DataTable in a Dataset or simply a DataTable.

    If a BindingSource is involved you can do

    Dim Index as Integer = SomeBindingSource.Find("FieldName","Value") -- thinking a string value.

    If Index is greater than -1 use SomeBindingSource.Postion = Index

    The above works if you connected a BindingSource to the DataSource of the DataGridView.

    If you are filtering data then we need to know more about how the data was initially loaded. But know that you can easily undue filtering in most cases with little code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by BB2017 Friday, April 7, 2017 7:08 PM
    Tuesday, March 28, 2017 7:06 PM
    Moderator
  • Hi BB2017,

    Based on your description, the code below will give you the GridView row index for the value.

    Dim searchValue As [String] = "somestring"
            Dim rowIndex As Integer = -1
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Cells(1).Value.ToString().Equals(searchValue) Then
                    rowIndex = row.Index
                    Exit For
                End If
            Next
    

    or you can use a LINQ query

    Dim rowIndex As Integer = -1
    
            Dim row As DataGridViewRow = dgv.Rows.Cast(Of DataGridViewRow)().Where(Function(r) r.Cells("SystemId").Value.ToString().Equals(searchValue)).First()
    
            rowIndex = row.Index
            dataGridView1.Rows(rowIndex).Selected = True

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BB2017 Friday, April 7, 2017 7:08 PM
    Wednesday, March 29, 2017 7:35 AM
    Moderator

All replies

  • Hi

    Maybe this veers towards your requirement. It shows a DataGridView filter in use. If you put the selection (cell) in a column then the Button1 click will filter the rows based on the contents of the TextBox1 matching the selected column cell contents.

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' and Button1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            With DataGridView1
                .DataSource = myTable
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            DataGridView1.DataSource = view
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Trim(TextBox1.Text).Length < 1 Or DataGridView1.SelectedCells.Count < 1 Then
                view.RowFilter = Nothing
                Exit Sub
            End If
            Dim c As Integer = DataGridView1.SelectedCells(0).ColumnIndex
            Filter(c)
        End Sub
        Sub Filter(c As Integer)
            Dim h As String = DataGridView1.Columns(c).Name
            Dim v As String = DataGridView1.SelectedCells(0).Value.ToString
            view.RowFilter = h & "='" & TextBox1.Text & "'"
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by BB2017 Friday, April 7, 2017 7:07 PM
    Tuesday, March 28, 2017 6:17 PM
  • Hello,

    The solution will differ depending on how you loaded the DataGridView e.g. using TableAdapter/DataSet/BindingSource or from a DataTable in a Dataset or simply a DataTable.

    If a BindingSource is involved you can do

    Dim Index as Integer = SomeBindingSource.Find("FieldName","Value") -- thinking a string value.

    If Index is greater than -1 use SomeBindingSource.Postion = Index

    The above works if you connected a BindingSource to the DataSource of the DataGridView.

    If you are filtering data then we need to know more about how the data was initially loaded. But know that you can easily undue filtering in most cases with little code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by BB2017 Friday, April 7, 2017 7:08 PM
    Tuesday, March 28, 2017 7:06 PM
    Moderator
  • Hi BB2017,

    Based on your description, the code below will give you the GridView row index for the value.

    Dim searchValue As [String] = "somestring"
            Dim rowIndex As Integer = -1
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Cells(1).Value.ToString().Equals(searchValue) Then
                    rowIndex = row.Index
                    Exit For
                End If
            Next
    

    or you can use a LINQ query

    Dim rowIndex As Integer = -1
    
            Dim row As DataGridViewRow = dgv.Rows.Cast(Of DataGridViewRow)().Where(Function(r) r.Cells("SystemId").Value.ToString().Equals(searchValue)).First()
    
            rowIndex = row.Index
            dataGridView1.Rows(rowIndex).Selected = True

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BB2017 Friday, April 7, 2017 7:08 PM
    Wednesday, March 29, 2017 7:35 AM
    Moderator
  • I appreciate all of the answers above.  As I read them they are all viable for what I need.  I will be trying them all.  Due to business expediency ("we wanted it yesterday"), I did a simple form.close without updating the sql table, then form.show which reopens/repopulates the datagrid with the original data.  I have "warning" flashing that if they close...the changes are also lost.  Again, thank you for the help.
    Friday, April 7, 2017 7:15 PM