Results 1 to 12 of 12

Thread: How to Search a DataGridView using TextBox?

  1. #1

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    How to Search a DataGridView using TextBox?

    So I'm using this code to search in all cells:

    Code:
    
    
        Private Sub Button38_Click_1(sender As Object, e As EventArgs) Handles Button38.Click
            Dim temp As Integer = 0
            For i As Integer = 0 To DataGridView1.RowCount - 1
                For j As Integer = 0 To DataGridView1.ColumnCount - 1
                    If DataGridView1.Rows(i).Cells(j).Value.ToString = TextBox1.Text Then
                        MsgBox("Item found")
                        temp = 1
                    End If
                Next
            Next
            If temp = 0 Then
                MsgBox("Item not found")
            End If
        End Sub

    It seems like it really founds many results but when it finishes the search it shows an Error message:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    System.Windows.Forms.DataGridViewCell.Value.get returned Nothing.



    How to solve this problem first?
    and if possible I want it to show only the rows containing the cell which has the same given text from Textbox1.

    I looked for a tutorial in code bank but wasn't lucky to find one, If there is one please provide me with.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to Search a DataGridView using TextBox?

    Firstly, you need to learn how to debug code. That's an essential skill for diagnosing any code issues. There's lots of information around about how to do it. You would set a breakpoint at the top of the code and step through it line by line, examining the state at each step. You can then compare what you expect to be to what actually is and see exactly what, when and where any differences are. In your case, the issue is fairly obvious and the information you posted is telling you what it is. It's telling you that getting the Value property of a cell is returning Nothing but in your code you're trying to call ToString on that result. If there's no object then there's no object to call ToString on, so you can't call it. This is an example of making sure that the code is doing what you expect. If you expect every cell to contain a value then you need to work out why there's a cell that doesn't. If you expect that some cells won't have values then you need to write code that takes that into account.

  3. #3

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by jmcilhinney View Post
    Firstly, you need to learn how to debug code. That's an essential skill for diagnosing any code issues. There's lots of information around about how to do it. You would set a breakpoint at the top of the code and step through it line by line, examining the state at each step. You can then compare what you expect to be to what actually is and see exactly what, when and where any differences are.
    I will make sure I will learn how to do that in a short time.




    Exactly What I need:
    Quote Originally Posted by jmcilhinney View Post
    If you expect that some cells won't have values then you need to write code that takes that into account.
    How to fix the code to take that into account?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to Search a DataGridView using TextBox?

    What did you come up with when you put some thought into it? One of the features of VB is that it reads like an English sentence so, if you're ever wondering how you should write your code, you should start by writing (or even just saying) a concise English sentence that describes what you want to achieve. That will often be very close to the actual code you need to write.

  5. #5

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by jmcilhinney View Post
    What did you come up with when you put some thought into it? One of the features of VB is that it reads like an English sentence so, if you're ever wondering how you should write your code, you should start by writing (or even just saying) a concise English sentence that describes what you want to achieve. That will often be very close to the actual code you need to write.
    I came up with the idea of using CStr that might handle Nulls

    Code:
     Private Sub Button38_Click_1(sender As Object, e As EventArgs) Handles Button38.Click
    
            Dim temp As Integer = 0
            For i As Integer = 0 To DataGridView1.RowCount - 1
                For j As Integer = 0 To DataGridView1.ColumnCount - 1
    
                    If CStr(DataGridView1.Rows(i).Cells(j).Value) = TextBox1.Text Then
                        DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(j)
                        MsgBox("Item found")
                        temp = 1
                    End If
    
                Next
            Next
            If temp = 0 Then
                MsgBox("Item not found")
            End If
        End Sub
    then another issue came up and trying my best to solve:


    System.InvalidCastException: 'Conversion from type 'DBNull' to type 'String' is not valid.'


    at this line of code:
    Code:
     If CStr(DataGridView1.Rows(i).Cells(j).Value) = TextBox1.Text Then
    trying to solve

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to Search a DataGridView using TextBox?

    It appears that you have a combination of different types of "blank" cells in your grid. If you're using ADO.NET, which will generally mean that your grid is bound to a DataTable, then you should be using DBNull.Value to represent a NULL value because that's how ADO.NET works. The reason for that is it works for reference types and values types, where Nothing can only value types if they are nullable. Nullable value types did not exist when ADO.NET was created, so Nothing could not be used to represent NULL. If your grid is unbound or bound to something other than a DataTable then you can use Nothing to represent a blank field.

    What may be happening is that your grid has a row at the bottom for the user to enter new data and that's where the Nothing values are coming from, while all the rows that are bound to an item in the data source contain DBNull.Value in blank cells. If that's the case then you shouldn't be including that last row in your search in the first place.

    This is exactly why you need to debug your code. Don't assume that your code is doing what you think it is. If that was the case then you wouldn't have an issue in the first place. You need to debug it to see what it actually is doing. When I said that you need to learn how to debug, I didn't mean some time in the future. I meant now.

  7. #7

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by jmcilhinney View Post
    It appears that you have a combination of different types of "blank" cells in your grid. If you're using ADO.NET, which will generally mean that your grid is bound to a DataTable, then you should be using DBNull.Value to represent a NULL value because that's how ADO.NET works. The reason for that is it works for reference types and values types, where Nothing can only value types if they are nullable. Nullable value types did not exist when ADO.NET was created, so Nothing could not be used to represent NULL. If your grid is unbound or bound to something other than a DataTable then you can use Nothing to represent a blank field.
    Yes I'm using ADO.NET and the whole grid is bound to an Access data table.
    Searching in how to use DBNull.Value?

    Quote Originally Posted by jmcilhinney View Post
    What may be happening is that your grid has a row at the bottom for the user to enter new data and that's where the Nothing values are coming from, while all the rows that are bound to an item in the data source contain DBNull.Value in blank cells. If that's the case then you shouldn't be including that last row in your search in the first place.
    This case is true but I need to know how to not include it from the same search code?

    Quote Originally Posted by jmcilhinney View Post
    This is exactly why you need to debug your code. Don't assume that your code is doing what you think it is. If that was the case then you wouldn't have an issue in the first place. You need to debug it to see what it actually is doing. When I said that you need to learn how to debug, I didn't mean some time in the future. I meant now.
    I need a keyword to start learning now.. something I colud type on youtube or google search to guide me to the correct place if possible, Anyway I'm starting now..

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by Joye View Post
    Yes I'm using ADO.NET and the whole grid is bound to an Access data table.
    Searching in how to use DBNull.Value?
    Calling ToString on a DBNull will return an empty String, so that means that an empty TextBox will match null values in your data. Your original code will work as intended as long as you only use it on rows that don't contain Nothing in a cell.
    Quote Originally Posted by Joye View Post
    This case is true but I need to know how to not include it from the same search code?
    It seems to me that you have just turned your brain off and aren't even bothering to think for yourself at this stage. You encountered a problem that you didn't know how to solve so you asked for help and that's fair enough. If someone gives you new information though, it's up to you to then use that information to try to solve the issue for yourself, rather than just wait for someone else to provide a turnkey solution. I just told you that you should not be searching the last row in the grid. Your code is searching every row in the grid. Maybe you should look at your code to see how it is doing that and think about what modification(s) would be required to exclude the last row.
    Quote Originally Posted by Joye View Post
    I need a keyword to start learning now.. something I colud type on youtube or google search to guide me to the correct place if possible, Anyway I'm starting now..
    You've already got a keyword. Do you think "debug" is not a keyword? If you want to learn how to debug VB.NET code then it should be obvious that you type "debug vb.net code" into a search engine. Again, you seem to have turned your brain off. People aren't always going to provide step-by-step instructions for you to follow or code that you can copy and paste. Many will be like me and provide as much information as you need to then solve the problem for yourself. It's up to you to use that information.

  9. #9

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by jmcilhinney View Post
    Calling ToString on a DBNull will return an empty String, so that means that an empty TextBox will match null values in your data. Your original code will work as intended as long as you only use it on rows that don't contain Nothing in a cell.

    It seems to me that you have just turned your brain off and aren't even bothering to think for yourself at this stage. You encountered a problem that you didn't know how to solve so you asked for help and that's fair enough. If someone gives you new information though, it's up to you to then use that information to try to solve the issue for yourself, rather than just wait for someone else to provide a turnkey solution. I just told you that you should not be searching the last row in the grid. Your code is searching every row in the grid. Maybe you should look at your code to see how it is doing that and think about what modification(s) would be required to exclude the last row.

    You've already got a keyword. Do you think "debug" is not a keyword? If you want to learn how to debug VB.NET code then it should be obvious that you type "debug vb.net code" into a search engine. Again, you seem to have turned your brain off. People aren't always going to provide step-by-step instructions for you to follow or code that you can copy and paste. Many will be like me and provide as much information as you need to then solve the problem for yourself. It's up to you to use that information.
    No No No I just needed a push to the right way..
    I think I'm on now and confident enough to go through..

    Thank you very much and I know you from a long time on this great forum and I already know that you will keep pushing me to the right way and that's why I keep asking about some more details to get some words to use and be confident to solve faster with more experience..

    I will solve it don't worry.. You gave me the things I needed to continue.. Just wait for me

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by Joye View Post
    I already know that you will keep pushing me to the right way
    Being pushy is just one of the services I offer.

  11. #11

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by jmcilhinney View Post
    Being pushy is just one of the services I offer.
    That worked

    I think the problem solved
    I just needed to set my datagridview with this behavior properties:
    AllowUserToAddRows set to False

    and my final code was :

    Code:
     Private Sub Button38_Click_1(sender As Object, e As EventArgs) Handles Button38.Click
            Dim temp As Integer = 0
            For i As Integer = 0 To DataGridView1.RowCount - 1
                For j As Integer = 0 To DataGridView1.ColumnCount - 1
                    If DataGridView1.Rows(i).Cells(j).Value.ToString = TextBox1.Text Then
                        DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(j)
                        MsgBox("Item found")
                        temp = 1
                    End If
                Next
            Next
            If temp = 0 Then
                MsgBox("Item not found")
            End If
        End Sub
    Thank you very much jmcilhinney
    This is great and my next problem is filtering the datagrdview to show only rows with matching texts from Textbox1.text.

    I'm suggesting this code but still having small issue in the part colored in green:

    Code:
    Private Sub Button43_Click_1(sender As Object, e As EventArgs) Handles Button43.Click
            Dim dv As DataView
            dv = New DataView(ESMC_REPAIR_COSTDataSet.Tables(0), "PART NUMBER" = TextBox1.Text, DataViewRowState.CurrentRows)
            DataGridView1.DataSource = dv
    
        End Sub

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to Search a DataGridView using TextBox?

    Quote Originally Posted by Joye View Post
    I just needed to set my datagridview with this behavior properties:
    AllowUserToAddRows set to False
    That's not really a solution unless you specifically don't want the user to be able to add rows, in which case you should have it set that way already. Look at your code:
    Code:
    For i As Integer = 0 To DataGridView1.RowCount - 1
    Can you think of any way to change that to only go to the second last row instead of the last one?
    Quote Originally Posted by Joye View Post
    This is great and my next problem is filtering the datagrdview to show only rows with matching texts from Textbox1.text.

    I'm suggesting this code but still having small issue in the part colored in green:

    Code:
    Private Sub Button43_Click_1(sender As Object, e As EventArgs) Handles Button43.Click
            Dim dv As DataView
            dv = New DataView(ESMC_REPAIR_COSTDataSet.Tables(0), "PART NUMBER" = TextBox1.Text, DataViewRowState.CurrentRows)
            DataGridView1.DataSource = dv
    
        End Sub
    You don't keep changing the DataSource all the time. You bind once and then filter in-place. If you bind the DataTable directly then the data you see already comes from a DataView; specifically the DefaultView property of the DataTable. If you want to filter then you set the RowFilter property of that DataView. Better yet, bind the DataTable to a BindingSource and bind that to the grid, then set the Filter property of that BindingSource.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width