搜索datagridview不工作

编程入门 行业动态 更新时间:2024-10-25 11:28:07
本文介绍了搜索datagridview不工作 - System.NullReferenceException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图通过我的datagridview上的第2列(数据类型:数字)运行搜索,但是不断收到以下错误消息:

I am trying to run a search through column 2 (data type: number) on my datagridview but keep getting the following error message:

An unhandled exception of type 'System.NullReferenceException' occurred in SpeedyRent.exe Additional information: Object reference not set to an instance of an object.

如果(!string.Equals(row。单元格[1] .Value.ToString(),driverNo.Text,StringComparison.OrdinalIgnoreCase))

The error is being thrown at if (!string.Equals(row.Cells[1].Value.ToString(), driverNo.Text, StringComparison.OrdinalIgnoreCase))

我做错了什么?我已经包括我的代码:

What is it that I'm doing wrong? I've included my code below:

void driverSearch() { CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource]; manager.SuspendBinding(); bool shouldNotFilter = string.IsNullOrEmpty(driverNo.Text); foreach (DataGridViewRow row in dataGridView1.Rows) { if (shouldNotFilter) { row.Visible = true; } else { if (!string.Equals(row.Cells[1].Value.ToString(), driverNo.Text, StringComparison.OrdinalIgnoreCase)) { row.Visible = false; } else { row.Visible = true; } } } manager.ResumeBinding(); } private void driverNo_KeyUp(object sender, KeyEventArgs e) { driverSearch(); } private void driverNo_TextChanged(object sender, EventArgs e) { driverSearch(); } private void driverNo_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; } driverSearch(); }

推荐答案

在foreach循环中尝试此块:

try this block in foreach loop:

if (shouldNotFilter) { row.Visible = true; } else { if(row.Cells[1].Value == null) { row.Visible = false; } else { if (!string.Equals(row.Cells[1].Value.ToString(), driverNo.Text, StringComparison.OrdinalIgnoreCase)) { row.Visible = false; } else { row.Visible = true; } } }

更多推荐

搜索datagridview不工作

本文发布于:2023-11-17 03:26:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!