Hi guys,

Im pretty new to C# and I have a problem hoping that most of you could solve cause you are quite an experienced developers :)
Here's my problem: I have a simple Windows Application Form with a dataGridView and a button. I want when the button is clicked to fill a simple dataTable and bind it to the dataGridView. The problem is that it always add the same row I mean it adds nine identical rows. But when I put a breakpoint at the end of the outer foreach() everything works great and i have different rows with different values that originlly comes from generateRandomValue(). I tried with row.AcceptChanges() because I think that it's caching row data but no luck again :(


Here's my code:

private void generateRandomGenes_Click(object sender, EventArgs e)
{


//header row
foreach (DataColumn item in adapter.Table.Columns)
{
DataColumn header = new DataColumn();
header.ColumnName = item.ColumnName;
header.DataType = item.DataType;
randomGenes.Columns.Add(header);

}



// Make data line
int row_number = 1;
for (int rows = 0; rows < 10; rows++)
{

DataRow row = randomGenes.NewRow();
row["Name"] = "artificial gene " + row_number;

int col_number = 1;

foreach (DataColumn column in randomGenes.Columns)
{

if (col_number < 21)
{
row[col_number] = generateRandomValue(adapter.Table, col_number);

col_number++;

}


}

randomGenes.Rows.Add(row);
row.AcceptChanges();

randomGenes.AcceptChanges();

if (row_number <= 9)
row_number++;

}


dataGridView1.DataSource = randomGenes;




}

Please, any suggestions. I'll really appreciate your help!

Recommended Answers

All 7 Replies

Maybe posting the rest of the code (or entire project) will help. The adapter.table is a bit of a mystery.
The acceptchanges is not really needed until later after the rows have all been added. I think the use of the adapter object in the for loop may be at the root of the problem, but until it is more obvious as to what that object is... it is just an assumption.

Maybe posting the rest of the code (or entire project) will help. The adapter.table is a bit of a mystery.
The acceptchanges is not really needed until later after the rows have all been added. I think the use of the adapter object in the for loop may be at the root of the problem, but until it is more obvious as to what that object is... it is just an assumption.

No, it's not neccesary you can try with some random data. You'll always get identical rows. adapter.table is fine, I use it in some other methods and everything works great. As I told you when you put a breakpoion at the end of the outer foreach and continue with F5, it's ok. Maybe the the changes of the row doesn't commit in runtime. I don't know, really :(

Paste the code for your generateRandomValue(); method. The problem is likely that when you have a break point that one second passes on your computers clock that changes the seed value for generateRandomValue(). You probably should use another GetRandomValue() method:

private void simpleButton4_Click(object sender, EventArgs e)
    {
      List<int> lst = new List<int>();
      for (int i1 = 0; i1 < 10; i1++)
      {
        lst.Add(GetRandom(10));
      }
      System.Diagnostics.Debugger.Break();
    }


    public static int GetRandom(int Maxvalue)
    {
      byte[] randomNumber = new byte[1];
      System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
      Gen.GetBytes(randomNumber);
      int rand = Convert.ToInt32(randomNumber[0]);
      return rand % Maxvalue + 1;
    }

I suspect you are using the "Random" class. This is a quote from the help file:

However, because the clock has finite resolution, creating different Random objects in close succession creates random number generators that produce identical sequences of random numbers. This problem can be avoided by creating a single Random object rather than multiple ones.

This would explain why debugger breaks would change the values since the clock would change in time between calls to generator.

Paste the code for your generateRandomValue(); method. The problem is likely that when you have a break point that one second passes on your computers clock that changes the seed value for generateRandomValue(). You probably should use another GetRandomValue() method:

private void simpleButton4_Click(object sender, EventArgs e)
    {
      List<int> lst = new List<int>();
      for (int i1 = 0; i1 < 10; i1++)
      {
        lst.Add(GetRandom(10));
      }
      System.Diagnostics.Debugger.Break();
    }


    public static int GetRandom(int Maxvalue)
    {
      byte[] randomNumber = new byte[1];
      System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
      Gen.GetBytes(randomNumber);
      int rand = Convert.ToInt32(randomNumber[0]);
      return rand % Maxvalue + 1;
    }

I suspect you are using the "Random" class. This is a quote from the help file:


This would explain why debugger breaks would change the values since the clock would change in time between calls to generator.

Here's mu function :

private double generateRandomValue(DataTable table, int col_number)
{
int index;
Random randomValue = new Random();
index = randomValue.Next(1, 3258);

DataRow row = table.Rows[index];
return ((double)row.ItemArray[col_number]);

}

I just use it cause I have to choose a random value from the current column. To be more clear I have a table with 3000 genes and based on that table I have to generate artificial profiles of new genes.
and i call it when i fill the data table like this :

row[col_number] = generateRandomValue(adapter.Table, col_number);

OK, then that is your problem. Change your random function to the one I posted and it should work fine.

OK, then that is your problem. Change your random function to the one I posted and it should work fine.

Thank you, I will try it now :)

yes, it worked. Thank you sooo much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.