Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi i need to copy the one gridview cell value to another gridview
eg:
gridview1:
a b c
1 s 2
2 v 3
3 d 4

The Second Gridview Value Should Be
a g h
1
1
2
2
2
3
3
3
but i am getting the null value(empty rows).
My Coding
ArrayList da1 = new ArrayList();
            ArrayList da2 = new ArrayList();
            int a1 = 0;
            int  a2 = 0;
           
         
         DataSet dc = (DataSet) Session["dtb"];
        //DataRow tr = (DataRow)Session["dg"];

        foreach (GridViewRow row1 in grv1.Rows)
        {
            a1 = int.Parse(row1.Cells[3].Text);
           
            da1.Add(a1);
                
        }
        int ri = 0;
        //int rd = 0;
        foreach (GridViewRow row1 in grv1.Rows)
        {
            
            //foreach (GridViewRow row2 in grv2.Rows)
            //{
            foreach (DataRow dr in dc.Tables[0].Rows)
            {
               //rd = int.Parse(dr.ToString());
                foreach (int ir in da1)
                {
                    
                  
                    for (; ri < ir; ri++)
                    {

                        //row2.Cells[1].Text = row1.Cells[1].Text;
                       dr[1] = row1.Cells[1].Text;
                     
                        

                    }

                    ri = ir;

                }

                //break; 
            }
            //break;

            //    continue;
        }

grv2.DataSource= dc;
                                
                 
        }
Posted
Updated 23-Feb-11 22:45pm
v17

1 solution

Few observations:
- variable id is initialized outside both loops and it's value is never changed inside the loops. However it's used to decide if row2 value will be set. Is this correct
- the inner loop goes through all source rows for each row in target gridview. Should the loops vice versa (loop the source and inside that add values in target loop).
 
Share this answer
 
Comments
gowdhami 19-Feb-11 3:50am    
i tried that what you are observed in my code. Still there is no change in my output
Wendelius 19-Feb-11 3:56am    
Have you tried debugging the code. If you go through it step-by-step, I think you'll discover the problem.
One more observation. I'm not familiar with ASP.NET objects but typically a grid is populated using some kind of datasource. Should you make modifications to the datasource (array, datatable etc) instead of directly modifying the gridview.

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