How do you search a DataGridView column for a specific value in code?

Hi Everyone,

I found out how to do it.

Instead of looping through the DataGridView, I used the DataTable that was used to populate the DataGridView.

I used the Find method of the DataTable.

Here is how I set everything up:

I placed the code to create the DataTable in a Class module.

Here's the code for that one:

VB.NET:
    Public Function GetDataTable(ByVal pSqlQuery As String, _
                                 ByVal pTableName As String, _
                                 ByVal pPrimaryKeyColumn As String) As DataTable

        ' A DataColumn must be an array.
        ' I need to find out if more then 1 column can be in this array so it can be used as the
        ' primary key for DataTable searches.
        '---------------------------------------------------------------------------------------
        Dim objKeyValueDataColumn(0) As DataColumn

        Try
            ' Create the connection object to use an SQL query and open it.
            '--------------------------------------------------------------
            objConnection = GetNutritionDataConnection()
            objConnection.Open()

            ' Create a DataTable object to hold data from the SQL query.
            '-----------------------------------------------------------
            objDataTable = New DataTable(pTableName)

            ' Create a DataAdapter object for the DataTable.
            '-----------------------------------------------
            objDataAdapter = New OleDbDataAdapter(pSqlQuery, objConnection)

            ' Load the DataAdapter with the data into the DataTable.
            '-------------------------------------------------------
            objDataAdapter.Fill(objDataTable)

            ' Set up the primary key for the data table.
            '-------------------------------------------
            objKeyValueDataColumn(0) = objDataTable.Columns(pPrimaryKeyColumn)
            objDataTable.PrimaryKey = objKeyValueDataColumn

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally
            ' Close the connection if it's currently open.
            '---------------------------------------------
            If objConnection IsNot Nothing Then
                objConnection.Close()
            End If
        End Try

        Return objDataTable
    End Function

I used this to create a DataColumn:
VB.NET:
        Dim objKeyValueDataColumn(0) As DataColumn

And I set up the primary key for finding with this code:
VB.NET:
            ' Set up the primary key for the data table.
            '-------------------------------------------
            objKeyValueDataColumn(0) = objDataTable.Columns(pPrimaryKeyColumn)
            objDataTable.PrimaryKey = objKeyValueDataColumn

The code I used this code to call the function to load data into my DataGridView:
VB.NET:
    Private Sub PopulateTheDataGridWithData()
        ' SQL for the DataGrid.
        '----------------------
        strSqlStatement = _
            "SELECT Format(INGREDIENT, ""Percent"") AS [Ingredient], " & _
                   "Format(PERCENT_FORM, ""Percent"") AS [Percent Form], " & _
                   "Percent_Form * 2000 AS [Ton Form], " & _
                   "Format(PROTEIN_VALUE, ""Percent"") AS [Protein Value], " & _
                   "Format(Percent_Form * Protein_Value, ""Percent"") AS [Protein Units], " & _
                   "Format(FAT_VALUE, ""Percent"") AS [Fat Value], " & _
                   "Format(Percent_Form * Fat_Value, ""Percent"") AS [Fat Units], " & _
                   "Format(FIBER_VALUE, ""Percent"") AS [Fiber Value], " & _
                   "Format(Percent_Form * Fiber_Value, ""Percent"") AS [Fiber Units], " & _
                   "Format(ASH_VALUE, ""Percent"") AS [Ash Value], " & _
                   "Format(Percent_Form * Ash_Value, ""Percent"") AS [Ash Units], " & _
                   "Format(COST_PER_POUND, ""Currency"") AS [Cost Per Pound] " & _
              "FROM Ingredients "

        ' Load the data into the grid with this query.
        '---------------------------------------------
        objIngredientsDataTable = objClassDatabaseObjects.GetDataTable(strSqlStatement, _
                                                                       "Ingredients Data", _
                                                                       "Ingredient")
        LightGridIngrediants.DataSource = objIngredientsDataTable

The user then changes the data in some text fields, then it gets saved back to the database. After the data is saved back to the database I refresh the DataGridView then re-display the data in the textboxes with this code that uses the Find method of the DataTable:

VB.NET:
    Private Sub LookupIngredientDetails()
        Dim objCurrentDataRow As DataRow

        ' Locate the Ingredient in the DataTable.
        '----------------------------------------
        objCurrentDataRow = objIngredientsDataTable.Rows.Find(strOriginalIngredient)

        ' Load up the fields on the form from the found DataRow.
        '-------------------------------------------------------
        EditBoxPercentForm.Text = objCurrentDataRow("Percent Form").ToString
        EditBoxProteinUnits.Text = objCurrentDataRow("Protein Units").ToString
        EditBoxProteinValue.Text = objCurrentDataRow("Protein Value").ToString
        EditBoxFatUnits.Text = objCurrentDataRow("Fat Units").ToString
        EditBoxFatUnits.Text = objCurrentDataRow("Fat Value").ToString
        EditBoxFiberValue.Text = objCurrentDataRow("Fiber Value").ToString
        EditBoxFiberUnits.Text = objCurrentDataRow("Fiber Units").ToString
        EditBoxAshValue.Text = objCurrentDataRow("Ash Value").ToString
        EditBoxAshUnits.Text = objCurrentDataRow("Ash Units").ToString
        EditBoxTonForm.Text = objCurrentDataRow("Ton Form").ToString
        EditBoxCostPerPound.Text = objCurrentDataRow("Cost Per Pound").ToString

    End Sub

It's probably not the most efficient way, but this VB newbie can use additional suggestions if you know of a better way.

Truly,
Emad
 
Back
Top