我在搜索datagridview时遇到错误 [英] I'm getting error on searching datagridview

查看:79
本文介绍了我在搜索datagridview时遇到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用文本更改事件从数据网格视图中搜索记录但是当我尝试此代码时

I want to search record from data grid view using Text change event but when I tried this code

string rowFilter = string.Format("SELECT * FROM [VisitorInfo] where First Name LIKE '%{0}%' AND Last Name LIKE '%{1}%'", F_Name.Text, L_Name.Text);
            (metroGrid1.DataSource as DataTable).DefaultView.RowFilter = rowFilter;





我收到



I'm getting

Object reference not set to an instance of an object

错误可以有人帮助我



我的尝试:



到目前为止我的代码

error can someone help me

What I have tried:

so far my codes

private void metroTile8_Click(object sender, EventArgs e)
       {
           try
           {
               Connection.Open();
               String str = "select * from [VisitorInfo]";
               OleDbCommand com = new OleDbCommand(str, Connection);
               DataSet ds = new DataSet();
               OleDbDataAdapter oledbda = new OleDbDataAdapter(com);
               oledbda.Fill(ds, "[VisitorInfo]");
               Connection.Close();
               metroGrid1.DataSource = ds;
               metroGrid1.DataMember = "[VisitorInfo]";
               metroGrid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
               metroGrid1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
               metroGrid1.DefaultCellStyle.Font = new Font("Tahoma", 15);
               metroGrid1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);
               metroGrid1.Columns[0].Visible = false;
               metroGrid1.Columns[0].FillWeight = 25;
               metroGrid1.Columns[1].FillWeight = 50;
               metroGrid1.Columns[2].FillWeight = 50;



               Connection.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void search_txt_TextChanged(object sender, EventArgs e)
       {
           string rowFilter = string.Format("SELECT * FROM [VisitorInfo] where First Name LIKE '%{0}%' AND Last Name LIKE '%{1}%'", F_Name.Text, L_Name.Text);
           (metroGrid1.DataSource as DataTable).DefaultView.RowFilter = rowFilter;
       }

推荐答案

使用调试器启动应用程序,在发生错误的行上设置断点,执行时停止,检查此行上使用的变量,其中一个不是您期望的对象。你需要找到原因。


使用调试器来查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Launch the app with debugger, set a breakpoint on line where error occurs, when execution stops, inspect variables used on this line, one of them is not the object you expect. You need to find the reason.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我在搜索datagridview时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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