| | |
Problem adding and saving changes to a dataRow in DataTable
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
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:
Please, any suggestions. I'll really appreciate your help!
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:
C# Syntax (Toggle Plain Text)
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!
Last edited by Tekmaven; Jun 15th, 2009 at 4:29 pm. Reason: Code Tags
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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.
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.
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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.
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:
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.
c# Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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:
c# Syntax (Toggle Plain Text)
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.
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.
![]() |
Similar Threads
- problem adding web reference for the web service located at server (ASP.NET)
- Problem in Adding records to MS-Access Database (VB.NET)
- Dreamweaver 8 - Password saving problem (Graphics and Multimedia)
- Problem in adding Hyperlinkcolumn (ASP.NET)
- Export DataTable to Excel with variable number of fields (VB.NET)
- Problem when adding delete confirmation (ASP)
- adding a bunch of List object to a set, and a bigger problem (Java)
- Having problems with adding a row to a DataSet (VB.NET)
- HELP!!! very strange problem! (Troubleshooting Dead Machines)
Other Threads in the C# Forum
- Previous Thread: Password Types?(md5)
- Next Thread: How can I refresh my discovered (using bluetooth windows stack) devices?
| Thread Tools | Search this Thread |
.net 2007 access algorithm array barchart bitmap box broadcast c# camera check checkbox client combobox control conversion cs4 csharp custom customactions database datagrid datagridview dataset date datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ handler httpwebrequest image index input install java keypress label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resolved. richtextbox search security server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml






