Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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.

  • *Experts*
Posted

Sure!

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]

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...