Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:30:00+00:00 2026-05-31T07:30:00+00:00

Im trying to implement a search function for when the user enters text in

  • 0

Im trying to implement a search function for when the user enters text in a textbox (tbPartNum) and then clicks the “Find” button it then searches the cells in dataGridView1 and once its found it, it highlights the entire row yellow. My code is as follows which obviously doesn’t work it throws an error which states:

“NullReferenceException was unhandled”

and underneath it:

“Object reference not set to an instance of an object.”

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace GBstock
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // populate the dataGridView with the Excel File
            string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", @"C:\Documents and Settings\rghumra\Desktop\Visual Studio\GBstock\GBstock\bin\Debug\FORM TEST.xlsx");
            string query = String.Format("select * from [{0}$]", "Sheet1");
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
            DataSet dataSet = new DataSet();
            dataAdapter.Fill(dataSet);
            dataGridView1.DataSource = dataSet.Tables[0];

            // populates the comboBox (cbSuppList) with all column headers
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                    cbSuppList.Items.Add(col.HeaderText);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            // Code to search the  alphanumneric Part Number (in Column1 header called "PART NUMBER") and highlihgt the row
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells["PART NUMBER"].Value.ToString().Equals(tbPartNum.Text))
                {
                    dataGridView1.Rows[row.Index].DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Instructions instructionForm = new Instructions();
            instructionForm.Show();
        }

        private void partToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewPart newPartForm = new NewPart();
            newPartForm.Show();
        }

        private void supplierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewSupplier newSuppForm = new NewSupplier();
            newSuppForm.Show();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:30:02+00:00Added an answer on May 31, 2026 at 7:30 am

    NullReferenceException you’re experiencing most likely comes from the fact that your grid contains null cell values, which get scanned in the foreach of your find handler. Try changing the following line:

    if (row.Cells["PART NUMBER"].Value.ToString().Equals(tbPartNum.Text))
    

    To

    var cellValue = row.Cells["PART NUMBER"].Value;
    if (cellValue != null && cellValue.ToString() == tbPartNum.Text)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a djapian based full text search for searching user
I've been trying to implement a delete function for a Binary Search Tree but
I'm trying to implement a Search-Function using c++ and libpqxx. But I've got the
I'm new to html and am trying to implement a search function. I've created
I'am trying to implement a most popular searches function on a web site using
I'm trying to implement a simple search function. I have a string array which
I'm working with SQL trying to implement a Full-Text-Search. I noticed that there is
I am trying to implement a search function on a database table using query
I'm trying to implement a venue search for Foursquare. I want the user to
I'm still trying to implement a search function into my Android app. So far

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.