【问题标题】:search for text in a cell of dataGridView and highlight the row?在 dataGridView 的单元格中搜索文本并突出显示该行?
【发布时间】:2012-03-24 12:07:55
【问题描述】:

我试图实现一个搜索功能,当用户在文本框 (tbPartNum) 中输入文本然后单击“查找”按钮时,它会搜索 dataGridView1 中的单元格,一旦找到它,它会将整行突出显示为黄色。我的代码如下,显然不起作用,它会抛出一个错误,指出:

“未处理 NullReferenceException”

在它下面:

“对象引用未设置为对象的实例。”

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

【问题讨论】:

    标签: c# search datagridview find


    【解决方案1】:

    NullReferenceException 您遇到的问题很可能是因为您的网格包含null 单元格值,这些值在您的查找处理程序的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)
    

    【讨论】:

    • @jimm_keen - 谢谢你的解释,它完美无缺。
    猜你喜欢
    • 2015-01-29
    • 2011-03-27
    • 2012-06-30
    • 1970-01-01
    • 2022-10-07
    • 2014-10-05
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多