Ankit  Shukla

Ankit Shukla

  • 1.3k
  • 668
  • 65.3k

Problem to set values in gridview control

May 18 2019 1:08 PM
Hello All,
 
I am facing a wieard problem. I am trying to insert random values in a label inside gridview.
When I use breakpoint and debug program, It works fine and shows different-2 values in all rows. But without debugging, it shows same values in all rows.
Design:
  1. <asp:GridView ID="GridView1" runat="server">  
  2. <AlternatingRowStyle Wrap="False" />  
  3. <Columns>  
  4. <asp:TemplateField HeaderText="StockNo">  
  5. <ItemTemplate>  
  6. <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>  
  7. </ItemTemplate>  
  8. </asp:TemplateField>  
  9. </Columns>  
  10. <EditRowStyle Wrap="False" />  
  11. <EmptyDataRowStyle Wrap="False" />  
  12. <FooterStyle Wrap="False" />  
  13. <HeaderStyle Wrap="False" />  
  14. <PagerStyle Wrap="False" />  
  15. <RowStyle Wrap="False" />  
  16. <SelectedRowStyle Wrap="False" />  
  17. <SortedAscendingCellStyle Wrap="False" />  
  18. <SortedAscendingHeaderStyle Wrap="False" />  
  19. <SortedDescendingCellStyle Wrap="False" />  
  20. <SortedDescendingHeaderStyle Wrap="False" />  
  21. </asp:GridView>  
Code:
  1. protected void Button6_Click(object sender, System.EventArgs e)  
  2. {  
  3. // foreach (GridViewRow row in GridView1.Rows)  
  4. for (int i = 0; i < GridView1.Rows.Count; i++ )  
  5. {  
  6. // Label lblSerial = (Label)GridView1.FindControl("Label4");  
  7. Label txtTotal = GridView1.Rows[i].Cells[0].FindControl("Label4"as Label;  
  8. Random random = new Random();  
  9. string r = "";  
  10. string N = "8988";  
  11. string K = "";  
  12. int j;  
  13. for (j = 1; j < 10; j++)  
  14. {  
  15. r += random.Next(0, 10).ToString();  
  16. }  
  17. K = N + r;  
  18. txtTotal.Text = K.ToString();  
  19. }  
  20. }  
Please help.

Answers (5)