Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.31/5 (3 votes)
See more:
I have a gridview in which at each row there is a dropdown list. I stuck to a point that I have to compare the values of these dropdown with each other. If the same value persist, then it will show error message otherwise it will continue.

Thanks in advance.
Posted
Updated 14-Jun-14 3:23am
v4

Hello Vijay, you have to loop through all the rows in your GridView control and compare if there is any duplicate value. Try the following code:
C#
bool foundDuplicate = false;
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
    DropDownList ddl1 = (DropDownList)GridView1.Rows[i].FindControl("ddlName");
    for (int j = i + 1; j < GridView1.Rows.Count; j++)
    {
        DropDownList ddl2 = (DropDownList)GridView1.Rows[j].FindControl("ddlName");

        if (ddl1.SelectedValue == ddl2.SelectedValue)
        {
            foundDuplicate = true;
            break;
        }
    }

    if (foundDuplicate == true)
    {
        break;
    }
}

Let me know if you have any further query.

- DD
 
Share this answer
 
Comments
[no name] 16-Jun-14 0:32am    
Thanks... It works
Debabrata_Das 16-Jun-14 0:33am    
You're WC :)
Hi Vijay, Why did you tried? How about compare in the Business Logic?

Regards.
 
Share this answer
 

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