5
Answers

Problem to set values in gridview control

Ankit  Shukla

Ankit Shukla

5y
589
1
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)
1
Amit Gupta

Amit Gupta

NA 22.9k 247.2k 5y
Random class doesn't guarantee to generate unique values its just to get random values. If you need all unique values then you must apply a validation im it.
0
Ankit  Shukla

Ankit Shukla

NA 681 121.7k 5y
I have already tried but no success.
0
Amit Gupta

Amit Gupta

NA 22.9k 247.2k 5y
Can you try clear and rebuild your project and clear cache of browser
0
Ankit  Shukla

Ankit Shukla

NA 681 121.7k 5y
It works fine when I debug code. But without debugging, all rows have same values.
0
Ankit  Shukla

Ankit Shukla

NA 681 121.7k 5y
But, if I am trying 20 times. Then at least 1 time it should work. And I am using difficult mechanism of random no. Which never gives same result in 20 tests.