Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to check before adding in the gridview if there are same values on the temporary gridview. and if there are atleast one same value it will stop the reading of for loop.

example :
textbox: 4
Column
2
3
4
[4]*stills add the value

in the first column. it will say "There has been same data on the list."
but stills enter the data "4" after 4 because it is not the same with 3 and 2.

VB
If grv_temporary.Rows(i).Cells(6).Text = TextBox1.Text Then
   msgbox(There has been same data on the list.")
Exit For
'do not continue process
Else
'add value

Thanks in advance guys. I know this is simple but i cannot get it.
Posted
Updated 24-Sep-14 17:03pm
v3
Comments
ChauhanAjay 24-Sep-14 22:42pm    
You could use the exit for if the condition is satisfied. Sorry for my last comment I misunderstood the code for c#.
NekoNao 24-Sep-14 22:58pm    
tried that. but it only exits on the first try but on the second try. it stills add .
ChauhanAjay 24-Sep-14 23:26pm    
Try to trim the values and check.
NekoNao 24-Sep-14 23:30pm    
What do you mean by trim the values?
NekoNao 24-Sep-14 23:57pm    
or can you give code for disabling the selected date?

1 solution

C#
boolean found = false;

foreach (DataGridViewRow row in grv_temporary)
{
    if (grv_temporary.Rows(i).Cells(6).Value == TextBox1.Text)
    {
        // row exists
        found = true;
        MessageBox.Show("Row already exists");
        break;
    }
}

if (!found)
{
    grv_temporary.Rows.Add(TextBox1.Text); // Your values here.
}



Try this. Read here[^] And Grid View add quantity if data already exists..[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900