none
Want to find match value from first column of DataGridView and make the row highlighted. RRS feed

  • Question

  • Hi,

    I'm working on a phase where I have Tables and some Queries. I have populated ComboBox from Query Data and when i select the required data by dropping down the ComboBox, I want the DataGridView to be highlighted the row which the match value exist. For Example my first column in Table is "RegNumber" and my ComboBox Display Member is "RegNumber" from Query. So if my selected value in ComboBox is "SC13-ADPCS-F18-001" and it should make search in Table's first column of "RegNumber" if value matches, it should highlight the following row.

            If Not String.IsNullOrWhiteSpace(RegNumberComboBox.Text) Then
                Dim index As Integer = DocSubBindingSource.Find("RegNumber", RegNumberComboBox.Text)
                If index > -1 Then
                    DocSubBindingSource.Position = index
                End If
            End If

    I've tried coding like this, As described by Miss Karen. I've tested this method in another simple project but not working in this one. 

    PS: I'm using different Forms to work with this project.

    Monday, June 10, 2019 7:42 PM

Answers

  • So, Finally i fixed it with another method. I used the Main DataBindingSource of the table to update the Checkbox state with the save button and refilled the query data to adjust filter setting. Finally it worked. Thanks for help!
    • Marked as answer by AHM3DRAXA Tuesday, June 11, 2019 3:35 PM
    Tuesday, June 11, 2019 7:27 AM

All replies

  • Hello,

    Where is this code being called from? What happens if you completely hard code the present code e.g.

    Dim pos = customerBindingSource.Find("CompanyName", "Around the Horn")
    customerBindingSource.Position = pos
    If the hard code fails then set a break point and step through the code and inspect values, when this is done did the event you have your code in hit? What value was returned for index?


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, June 10, 2019 9:31 PM
    Moderator
  • I tried using the static code. Actually i am using it with ComboBox_TextChanged Event to do this. When i tried it in the sample you made, i've downloaded and implemented the method within the ComboBox Textchanged event and it worked perfect there. But not working here even in hard coded method.
    Tuesday, June 11, 2019 4:52 AM
  • Hi,

    set the background color to highlight

    Imports System.Data.SqlClient
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As New DataTable
        Dim bind As New BindingSource
        Dim j As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With DataGridView1
                .Columns.Add(New DataGridViewTextBoxColumn With {.Name = "RegNumber"})
                .Columns.Add(New DataGridViewTextBoxColumn With {.Name = "Name"})
                .Columns.Add(New DataGridViewTextBoxColumn With {.Name = "ClassId"})
                .Rows.Add(New Object() {1, "alex", 3})
                .Rows.Add(New Object() {2, "zack", 1})
                .Rows.Add(New Object() {3, "peter", 2})
                .Rows.Add(New Object() {4, "jack", 4})
                .Rows.Add(New Object() {5, "jimmy", 2})
            End With
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From Student", conn)
                sda.Fill(dt)
                ComboBox1.DataSource = dt
                ComboBox1.DisplayMember = "Id"
                ComboBox1.ValueMember = "Id"
            End Using
            AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
    
            DataGridView1.Rows(j).DefaultCellStyle.BackColor = Color.White
            For i = 0 To DataGridView1.Rows.Count - 1
                If DataGridView1.Rows(i).Cells(0).Value.ToString = ComboBox1.SelectedValue.ToString Then
                    DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
                    j = i
                    Exit For
                End If
            Next
    
        End Sub
    
    
    End Class

    Best Regards,

    Alex


    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.


    Tuesday, June 11, 2019 5:39 AM
  • Actually I'm not using the static data to be searched. I have linked another Query as DataBindingSource to the ComboBox.

    The Detailed story is:

    I have content of table are like;

    1. RegNumber (Short Text)

    2. Name (Short Text)

    3. Class (Short Text)

    4. IsBatched (Yes/No)

    similar to that i have made a Select Query which displays the unchecked (IsBatched = No) results. So, I have attached the query to the combobox. I want to to be when i have selected and unchecked result from ComboBox, it should find the result in the Table and on press of save button it should check the ("IsBatched) and the current unchecked result should be checked and disappears from the Query.

    Tuesday, June 11, 2019 6:02 AM
  • So, Finally i fixed it with another method. I used the Main DataBindingSource of the table to update the Checkbox state with the save button and refilled the query data to adjust filter setting. Finally it worked. Thanks for help!
    • Marked as answer by AHM3DRAXA Tuesday, June 11, 2019 3:35 PM
    Tuesday, June 11, 2019 7:27 AM
  • So, Finally i fixed it with another method. I used the Main DataBindingSource of the table to update the Checkbox state with the save button and refilled the query data to adjust filter setting. Finally it worked. Thanks for help!

    Hi,

    I am glad you have got your solution, we appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    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.

    Tuesday, June 11, 2019 9:06 AM