search for text in a cell of dataGridView and highlight the row?

戏子无情 提交于 2019-12-25 00:06:05

问题


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 doesnt 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:


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)


来源:https://stackoverflow.com/questions/9593425/search-for-text-in-a-cell-of-datagridview-and-highlight-the-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!