使用文本框C#进行搜索时出现DefaultView.RowFilter错误 [英] DefaultView.RowFilter error when search with textbox c#

查看:309
本文介绍了使用文本框C#进行搜索时出现DefaultView.RowFilter错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
在文本框中进行搜索时出现错误,更具体地说是这样:
-搜索test1(记录在数据表上就可以了)
-搜索Test1(总是可以)
-搜索Test5或记录中没有数据表的任何位置的任何单词都会给我带来systemoutofexception.
这是代码中我看到错误的部分:

Hello,
I've a error when searching through the textbox, more specifically like this:
-Searching test1 (the record is on the datatable so is ok)
- Searching Test1 (always ok)
- Searching Test5 or any word in any position that doesn't datatable have in the records give me a systemoutofexception.
This is the part of the code where i see the error:

dttest.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", filterField, textBoxId.Text);
            MemoryStream ms = new MemoryStream();
            Bitmap img = (Bitmap)dataGridView1.CurrentRow.Cells[1].Value; <--there become yellow
            img.Save(ms, ImageFormat.Jpeg);
            pictureBox1.Image = Image.FromStream(ms);

因此,我认为搜索存在问题,因为我不喜欢当存在不适合记录的字符时不执行任何操作".但是我有一个问题(如果这是问题)我怎么能把它放在c中呢?

So i think it's a problem with the searching because i didn't put like "do nothing when there's a character that doesn't suit the record" but i have a problem (if this is the problem) how can i put this in c ?

推荐答案

James

由于可能找不到过滤器的结果,因此您将获得异常.

Since the result of your filter might be nothing to be found, so you will get an exception.

您可以轻松地放置一个if语句,以指示DefaultView中是否还有任何数据,如果有,那么您可以继续执行您想做的事情,如果没有,则什么也不做或做其他事情.

You could easily put a if statement to indicate if there is any data left in your DefaultView, if there is, then you could proceed what you want to do, if not, then do nothing or do something else.

例如:

dttest.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", "Name", 123);

if( dttest.DefaultView.Count > 0 )
{
    MemoryStream ms = new MemoryStream();
    Bitmap img = (Bitmap)dataGridView1.CurrentRow.Cells[1].Value;
    img.Save(ms, ImageFormat.Jpeg);
    pictureBox1.Image = Image.FromStream(ms);
}


希望对您有帮助.


Hope this will be helpful to you.


这篇关于使用文本框C#进行搜索时出现DefaultView.RowFilter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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