Generate Data for Display [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Given the following semi-code:
string[] MachineNames = new String[#ofMachines];
object[,] objectArray = new object[#ofSettings, #ofMachines];
int iSettingIndex = 0;

foreach (Setting in Settings)
{
for (int iMachineIndex = 0; iMachineIndex < #ofMachines; iMachineIndex++)
{
MachineNames[iMachineIndex] = getName(iMachineIndex);
objectArray[iSettingIndex, iMachineIndex] = getValue(iMachineIndex, strPath, strKey);
}
iSettingIndex++;
}

This will generate a 2Dimension Object Array, which would resemble something like:

Index 1 2 3
1 X Y Z
2 X Y Z
3 X Y Z
Where X,Y, and Z are the values for each setting on each Robot

My goal is to take this code and overlay it all in a Datagrid of some kind.

This requires 2 changes:
a) I need to add a Row0 [for the actual Machine names and not just the index] and a Column0 [for the actual Setting name (path+key) and not just the index]

b) I need to somehow set this 2 dimensional Object Array as the datasource for my Datagrid [when I do it directly C# gives me an error stating that: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Array was not a one-dimensional array.]

The end results should look like [in a Datagrid of some kind, without the Index information]:

Index 1 2 3
1 X Robot1 Robot2
2 \\Path1\Key1 X Y
3 \\Path2\Key2 X Y
4 \\Path3\Key3 X Y
 
If you're going to store information in a table, it makes much more
sense to use an instance of a [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/vbcon/html/vboriDatasets.htm]DataSet[/mshelp] or a [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconworkingwithtypeddataset.htm]strongly-typed
DataSet[/mshelp] to store your data. Then it's a piece of cake to display in
a DataGrid (or any other data control); just set the DataSource
and DataMember properties and call DataBind(). Plus, you can
easily serialize the data into an XML file to save your settings.
 
I agree, using a Dataset would be ideal [no clue about DataBind, never tried that before], I have often used Datasets when dealing with SQL [to display results in Datagrids] however this is rather different seeing as the results are generated dynamically [and not just a Dataset.fill].

But for example, forget the Foreach loops, if I wanted to generate a dataset to fill my datagrid to look like the following:

Robot1
Key1 X

How could I do that? [given the values are hardcoded].
How do I fill the Dataset by saying, use "Robot1" as Column1, "Key1" as Row1, and "X" as the result?
And then: Datagrid.datasource = Dataset

If I can do that then filling the Dataset dynamically should be simple enough.
 
Sure!
Visual Basic:
Dim data As New DataSet()
Dim table As New DataTable()

' Add the columns
With table.Columns
  .Add(New DataColumn("Index"))
  .Add(New DataColumn("Value 1"))
  .Add(New DataColumn("Value 2"))
  .Add(New DataColumn("Value 3"))
End With

' Add the table to the dataset
data.Tables.Add(table)

' Get the row and set the values
Dim row As DataRow = table.NewRow()
row(0) = "Robot1"
row(1) = "x"
row(2) = "y"
row(3) = "z"
table.Rows.Add(row)

And there you have it. Simple, eh?

[mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconcreatingusingdatasets.htm]Read more![/mshelp]
 
Back
Top