dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Check if value in TextBox exist in DatagridView

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    46

    Resolved [RESOLVED] Check if value in TextBox exist in DatagridView

    Hello every one
    Please if you can help me to repair this code ..
    I wont check if value in TextBox1 Or TextBox2 Or TextBox3 Or TextBox4 Or TextBox5 exists in DatagridView1 before printing
    This code work very well with a single TextBox1 ..
    Code:
        Dim IsFound As Boolean = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                For i As Integer = 0 To DataGridView1.Columns.Count - 1
                    If row.Cells(i).Value.ToString = TextBox1.Text Then
                        IsFound = True
                        Exit For
                    End If
                Next
            Next
            If (IsFound) Then
                MessageBox.Show("Error .. Value already exist in DataGridView1")
            Else
       .......Code Printing
        End If
    But i have 5 TextBox .. and when i tried with this code like this .. it does not work :
    Code:
        Dim IsFound As Boolean = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                For i As Integer = 0 To DataGridView1.Columns.Count - 1
                    If row.Cells(i).Value.ToString = TextBox1.Text Or TextBox2.Text Or TextBox3.Text Or TextBox4.Text Or TextBox5.Text Then
                        IsFound = True
                        Exit For
                    End If
                Next
            Next
            If (IsFound) Then
                MessageBox.Show("Error .. Value already exist in DataGridView1")
            Else
       .......Code Printing
        End If
    Thank you in advance for help
    ABE

  2. #2
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    788

    Re: Check if value in TextBox exist in DatagridView

    Check if variable a is equal to one of several different values:

    Code:
    If a = 1 OrElse a = 2 OrElse a = 3 OrElse a = 4 ...
    Compare that to your code and you should see how to fix your If statement.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    46

    Re: Check if value in TextBox exist in DatagridView

    Thank you OptionBase1
    I tried like this :
    Code:
       If row.Cells(i).Value.ToString = TextBox1.Text OrElse TextBox2.Text OrElse TextBox3.Text OrElse TextBox4.Text OrElse TextBox5.Text Then
    But always it does not work
    Thank you
    ABE

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,729

    Re: Check if value in TextBox exist in DatagridView

    Quote Originally Posted by ABIDINE View Post
    Thank you OptionBase1
    I tried like this :
    Code:
       If row.Cells(i).Value.ToString = TextBox1.Text OrElse TextBox2.Text OrElse TextBox3.Text OrElse TextBox4.Text OrElse TextBox5.Text Then
    But always it does not work
    Thank you
    ABE
    But that's not what you were shown. Take another look at the code OptionBase1 provided and read it properly this time. I'll give you a clue: how many '=' operators do you have in your code?

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    46

    Re: Check if value in TextBox exist in DatagridView

    Thank you vey much OptionBase1 for idea
    Thank you very much jmcilhinney for orientation
    Now it work very well like this
    Code:
    If row.Cells(i).Value.ToString = TextBox1.Text OrElse row.Cells(i).Value.ToString = TextBox2.Text OrElse row.Cells(i).Value.ToString = TextBox3.Text OrElse row.Cells(i).Value.ToString = TextBox4.Text OrElse row.Cells(i).Value.ToString = TextBox5.Text Then
    Cordially
    ABE

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,729

    Re: [RESOLVED] Check if value in TextBox exist in DatagridView

    For the record, you should not execute the same complex expression over and over, for the sake of efficiency and readability. In your case, that means not using row.Cells(i).Value.ToString over and over. Execute that expression once and assign the result to a variable, then use that variable everywhere you need that value. You also have the option of putting all the values into an array or collection and then calling Contains on that to see if it contains the cell value.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    46

    Re: [RESOLVED] Check if value in TextBox exist in DatagridView

    Thank you very much
    How to do ( row.Cells (i) .Value.ToString ) in a single variable in my code ..in Module ??
    Cordially

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,729

    Re: [RESOLVED] Check if value in TextBox exist in DatagridView

    Why are you trying to complicate it? Declare a variable and assign the appropriate value to it. You obviously know how to do that because you're already doing it in the code you posted.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width