I’m working on a project writing an OPC client (a standard in process engineering) and I’ve come across a little datadesign issue that I could really use your opinions on! Basically the way the OPC server works is you query using a Synchronous read method and essentially after some back of fourth it returns a lot of values in a single messy System.Array, the nature of the client means that this array can be anything from 6 objects to over a thousand or more.
Luckily, as that has to be done is display this allready optimized data in a GridView, but this is where I’m not sure what the best way to do it is. The amount of columns is not dynamic, so that helps a lot but basically I need it to iterate through the Array, insert it into the Grid creating a new row after each 6 values.
My first approach was to write a method that splits the initial Array into a lot of small [6]arrays but surely there’s an easier way? I’ve also been looking into the BindingList object, it seems like something worth using? or maybe a double for-loop?
So in short, I have a dynamic array of values and for each 6 values I need to insert them into a row in a DataTable/GridView - so row1: v1, v2, v3, v4, v5, v6 /new row v7, v8, v9, v10, v11, v12 and so on unto the breach.
Key question is “is this an array of objects or a multi-dimensional array?”
If it is the former, it is easy – just set the datasource property and call DataBind() and you are set, just like binding to any other collection of objects.
Multi-dimensional is a better question – I’d be inclined to write a wrapper that pushes the multi-dimensional array into a layout object and then bind to the object. Also opens the door for stuff like creating formatting without gads of ASPX template code.
I ended up cutting the massive Array into small arrays of a set length, and then adding the small array into the DataTable using the .loadDataRow(object, boolean) method.
If anyone should ever need it, here’s my final code (C#):
// Cut the values array after each row length and generate a row in the grid
var fifties = from index in Enumerable.Range(0, lol.Length)
group lol[index] by index / 7;
int numRows = 1;
foreach (var fifty in fifties)
{
if (numRows <= CVcount)
{
dt.LoadDataRow(fifty.ToArray(), true);
numRows++;
}
else
{
dmvt.LoadDataRow(fifty.ToArray(), true);
}
}
// refresh the Grids
CVGrid.DataBind();
MVGrid.DataBind();
Interesting – where did you get that exception? You can definitely databind to anything that implements IEnumerable (and a few other interfaces) so System.Array should work . . .