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!