在dataGridView的单元格中搜索文本并突出显示该行? [英] search for text in a cell of dataGridView and highlight the row?

查看:295
本文介绍了在dataGridView的单元格中搜索文本并突出显示该行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个搜索功能,当用户在文本框中输入文本(tbPartNum),然后单击查找按钮,然后搜索dataGridView1中的单元格,一旦找到它,它会突出显示整行黄色。我的代码如下,这显然不工作,它会抛出一个错误,指出NullReferenceException是未处理的,并在其下面对象引用未设置为对象的实例。

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

命名空间GBstock
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent() ;

//使用Excel文件填充dataGridView
字符串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];

//用所有列标题填充comboBox(cbSuppList)
foreach(dataGridView1.Columns中的DataGridViewColumn列)
{
cbSuppList.Items.Add(col。的HeaderText);



private void btnFind_Click(object sender,EventArgs e)
{
//用于搜索字母部件号的代码(row.Cells [PART NUMBER]。Value.ToString()。这个函数的名字是PART NUMBER,而highlihgt是行
foreach(DataGridViewRow row dataGridView1.Rows中)
{
if(row.Cells [PART NUMBER]。 Equals(tbPartNum.Text))
{
dataGridView1.Rows [row.Index] .DefaultCellStyle.BackColor = Color.Yellow;



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

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

$ b private void supplierToolStripMenuItem_Click(object sender,EventArgs e)
{
NewSupplier newSuppForm = new NewSupplier();
newSuppForm.Show();






$ div您遇到的最有可能来自您的网格包含 null的事实单元格值,它们在find处理程序的 foreach 中扫描。尝试更改以下行:

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

  var cellValue = row.Cells [PART NUMBER]。Value; 
if(cellValue!= null&& cellValue.ToString()== tbPartNum.Text)


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();
    }
}

}

解决方案

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)

这篇关于在dataGridView的单元格中搜索文本并突出显示该行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆