在datagridview中搜索特定的文件[VB.NET] [英] Search a particular fied in a datagridview[VB.NET]

查看:132
本文介绍了在datagridview中搜索特定的文件[VB.NET]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个小问题我无法解决。我有一个包含多个列和行的Datagridview。我使用带有项目编号的文本框进行搜索,以显示完整的行。这是我的代码:

  Dim 找到 As  布尔 = 错误 
尝试
' 搜索我的datagridview中的所有行
对于 每个作为 DataGridViewRow data_item.Rows
' 如果colomns NumeroItem =我的文本框.. .find = true
如果(row.Cells.Item( < span class =code-string> NumeroItem)。Value.ToString = txt_search.Text)然后
found = 正确
结束 如果

< span class =code-keyword> Next
Catch ex As 异常
MsgBox(ex.Message)
结束 尝试
如果找到
' 过滤以过滤文本框的包含
DataTable1.DefaultView.RowFilter = NumeroItem LIKE'*& txt_search.Text& *'
结束 如果





我的比较做到了我想要的,当它到达时在正确的价值,它进入我的条件。但是,他发现了类似这样的错误:对象引用未设置为对象的实例。我不明白为什么?



如果有人能帮助我,那真的很棒!



我尝试了什么:



我尝试调试它并逐行观看,但没有错! :(

解决方案

确保 txt_search.Text 始终具有某个值,您输入的值或默认值[例如txt_search.Text =没有]。



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

这通常发生在那里是否有数据可以进一步传递以进行处理但是系统无法理解传递它的位置,除非我们明确地处理它。



很好。你正在使用Try Catch& Debugger。还使用 ELSE 条件来跟踪代码无法继续的位置,并执行一些头脑风暴以节省大量时间进行调试。< br $>




快乐编程......;)





问候,

Mayur Dighe

如何编码:


实现这一目标的方法很少,例如:



  1. 如何:使用以下方法对ADO.NET数据进行排序和过滤Windows窗体绑定源组件 [ ^ ]
  2. 如何:直接在数据表中筛选和排序 [ ^ ]
  3. DataView.RowFilter属性(System.Data) [ ^ ]
  4. DataTable.Select Method(String)(System.Data) [ ^ ]


< blockquote>

Quote:

我尝试调试它并逐行观看,但没有错! :(



你在这里因为你知道出了什么问题,你只是找不到什么问题。



再次转到调试器

在代码中添加断点

打开'局部变量'窗口



as你逐行执行你的代码,检查所涉及的每个变量的包含和类型。你可能会发现例如 DataTable1 不是你所期望的。


Hi guys,
I have a small problem I can not solve. I have a Datagridview with multiple columns and rows. I search with a textbox with the item number to display the complete row . Here is my code:

Dim found As Boolean = False
     Try
         'search in all the rows in my datagridview
         For Each row As DataGridViewRow In data_item.Rows
             'if the colomns NumeroItem = my textbox... found = true
           If (row.Cells.Item("NumeroItem").Value.ToString = txt_search.Text) Then
                 found = True
             End If
 
         Next
     Catch ex As Exception
         MsgBox(ex.Message)
     End Try
     If found Then
         'Filter to filt the contain of the textbox
         DataTable1.DefaultView.RowFilter = "NumeroItem LIKE '*" & txt_search.Text & "*'"
     End If



My comparison does what i want properly, when it arrives at the right value, it enter in my condition. but, he catch an error something like this: Object reference not set to an instance of an object. I do not understand why?

If there is anyone who could help me it would be really nice!

What I have tried:

I try to debug it and watch line by line, but nothing is wrong! :(

解决方案

Make sure that txt_search.Text is always have some value, either entered by you or default value[e.g. txt_search.Text = "nothing"].

Object reference not set to an instance of an object.

This usually occurs when there is data available to pass further for processing BUT system doesn't understand WHERE to pass it, unless we explicitly Take Care of it.

It's Good. You are using Try Catch & Debugger. Also use ELSE condition to track where the code fails to continue and do some Brainstorm to save much time in doing debug.


Happy Programming... ;)


Regards,
Mayur Dighe
How To Code For:


There's few ways to achieve that, for example:


  1. How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component[^]
  2. How to: Filter and Sort Directly in Data Tables[^]
  3. DataView.RowFilter Property (System.Data)[^]
  4. DataTable.Select Method (String) (System.Data)[^]


Quote:

I try to debug it and watch line by line, but nothing is wrong! :(


You are here because you know that something is wrong, you just don't find what is wrong.

Go to debugger again
put a breakpoint in your code
open 'local variables' window

as you execute your code line by line, check the contain and type of every variable involved. you may find for example that DataTable1 is not what you expect.


这篇关于在datagridview中搜索特定的文件[VB.NET]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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