views:

362

answers:

2

I'm looking for a highly efficient way to save data to a table like structure in C#. The number of rows, columns and data type for each column is determined at runtime. One of my approaches is to create a struct that defines a column. The struct consists of ColumnName, Units, and a generic List of doubles. (For now I am sticking to one data type). I then instantiate a struct for each column and fill its list. This method works pretty well and is much cleaner than some other methods we've tried, I am able to save 100,000 rows of 3 columns in about 12 milliseconds. The problem I am having is displaying the data. I am able to display my "table" in a datagridview by looping through my columns and rows and filling the DGV cell by cell, but this is, not suprisingly, very slow. Is there a way to use my 3 lists as a datasource for the DGV? Can each column have it's own datasource? Is the an alternative to the DataGridView that I should try?

+1  A: 

Have you tried filling a DataTable first and then binding that DataTable to the DataGridView?

If it's a WebApplication you can do a foreach statement and build a table from code-behind.

GxG
See comment above re: DataTable
Dan Schubel
+1  A: 

What are the "3 lists" here? I can see the list of doubles... if the columes are dynamic, then implementing ITypedList might be useful, but I'll be honest - it is a pig to implement correctly; it would be easier to fill a DataTable. I've used this, for example, to transpose an array on the fly.

If performance is your concern, perhaps switch to virtual mode? This would appear to match your use-case quite well.

Marc Gravell
Each of the 3 lists represents a column. My test app contains a textbox for the user to enter the number of rows and columns and I've been using 3 X 100,000 typically. Real world could be more like 8 x 1,000,000.My first attempt was to simply write data directly to a DataTable but performance was not great with large # of rows.Copying my the contents of my lists to a DataTable and using that as the DGV datasource is quicker than going cell by cell, but could present some other challanges.I'll have to investigate ITypedList and VirtualMode.
Dan Schubel
My first choice would be virtual mode. I have some `ITypedList` examples kicking around on usenet if you want.
Marc Gravell
For example, http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/ae1251a6855b9254/f5599f934a2f7043
Marc Gravell
Got it working using virtual mode, thanks for the help.
Dan Schubel