Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi how to search the numeric value in gridview.

C#
public void sa(GridView gv1)
{
    int c1 = 0;
    int cv1 = 0;
    int ans;
    foreach (GridViewRow row in gv1.Rows)
    {
        cv1 = System.Convert.ToInt32(row.Cells[4].Text);
        c1 += currentValue1;
    }
    ans = c1;
        Random RandomClass = new Random();
        int RandomNumber = RandomClass.Next(1, ans);
        Response.Write(RandomNumber);
        foreach (GridViewRow r in gv1.Rows)
        {
            gv1.FindControl(ans.ToString());
        }
}
Posted
Updated 9-Feb-11 19:11pm
v4
Comments
Sandeep Mewara 10-Feb-11 0:51am    
Did you try anything?
gowdhami 10-Feb-11 0:57am    
ya but only searching from the database but i dont have any database
[no name] 10-Feb-11 0:56am    
What code you wrote or you want your homework to be done by us?
Sunasara Imdadhusen 10-Feb-11 1:12am    
Added code formatting!
Sunasara Imdadhusen 10-Feb-11 1:12am    
What is your problem?

1 solution

Hi Gowdhami,

In your loop calculating the cf, store the cf as key and row index as value in a dictionary. Later once the random number generated check the dictionary whether the random number matches with any cf..(just use the contains(key) method to match). If the random number exists you will get the row index. Using the result row index you can get the row from the gridview then the desire cell or the value in it.

Does this help?

Note: Random number may not exactly match in the cf collection. Instead you want to find the nearest value?.
 
Share this answer
 
Comments
gowdhami 10-Feb-11 3:39am    
hey thanks for your answer somewhat it helps for me.. i have to find the nearest value.. thanks a lot
Albin Abel 10-Feb-11 11:07am    
In that case just go back n forth from your random number till it results true for contains(key)

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