0

How can I load a Data from XML file to DataGridView like the table below:

y\x  [0,0]          0.000976563 0.000976563 0.000976563 0.000976563 0.000976563 
y\x  [10,10]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563 
y\x  [20,20]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563 
y\x  [30,30]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563 
y\x  [40,40]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563 

The structure of my XML file is:

  <?xml version="1.0" standalone="yes"?>
        <Tag>
          <Parameter>
            <Name>Parameter1</Name>
            <Size_X>7</Size_X>
            <Size_Y>2</Size_Y>
            <Value_X>0;2;8;12;14;16;19;</Value_X>
            <Value_Y>-20;-10;</Value_Y>

<Value>0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;25.0000000000000000;0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;25.0000000000000000;</Value>
          </Parameter1>
          <Parameter2>
            <Name>Parameter2</Name>
            <Size_X>7</Size_X>
            <Size_Y>3</Size_Y>
            <Value_X>0;2;4;6;8;10;12;</Value_X>
            <Value_Y>20;40;60;</Value_Y>
<Value>0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;     0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;</Value>
              </Parameter2>
            </Tag>

I Have tried the next code, but it does not work:

XDocument xmlDoc = XDocument.Load("C:\\Write.xml");
            var variables = from variable in xmlDoc.Descendants("Parameter")
                            where variable.Element("Name").Value == "Parameter1"
                            select new
                            {
                                Name = variable.Element("Name").Value,
                                SizeX = variable.Element("Size_X").Value,
                                SizeY = variable.Element("Size_Y").Value,
                                ValueX = variable.Element("Value_X").Value,
                                ValueY = variable.Element("Value_Y").Value,
                                Value = variable.Element("Value").Value,
                            };

            foreach (var variable in variables)
            {

                var x = Convert.ToInt32(variable.SizeX);
                var y = Convert.ToInt32(variable.SizeY);
                dataGridView1.TopLeftHeaderCell.Value = "y/x";

                var arr = new double[y, x];//{Value} it must be the value here???
                var columnCount = arr.GetUpperBound(1) + 1;
                var rowCount = arr.GetUpperBound(0) + 1;

                for (int i = 0; i < columnCount; i++)
                {
                    dataGridView1.Columns.Add(i.ToString(), variable.ValueX);
                }
                for (int i = 0; i < rowCount; i++)
                {

                    dataGridView1.Rows.Add(i.ToString(),variable.ValueY);
                    for (int k = 0; k < columnCount; k++)
                    {
                        dataGridView1.Rows[i].Cells[k].Value = arr[i, k];
                    }

                }

2 Answers 2

1

First add your columns and rows then populate the cells

var arr = new int[5, 3];  // array is just for example
var columnCount = arr.GetUpperBound(1)+1;
var rowCount = arr.GetUpperBound(0)+1;

for (int i = 0; i < columnCount; i++)
{
    dataGridView1.Columns.Add(i.ToString()," ");
}
for (int i = 0; i < rowCount; i++)
{
    dataGridView1.Rows.AddCopy(0);
    for (int k = 0; k < columnCount; k++)
    {
        dataGridView1.Rows[i].Cells[k].Value = arr[i, k];
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

This article Binding a two dimensional array to a DataGrid will give you some insight. Basically:

//
// 1. Create two dimensional array
//
const int  dim = 1000;

double[,]  array = new double[dim,dim];

Random ran = new Random();
for(int r = 0; r < dim; r++)
{
    for(int c = 0; c < dim; c++)
    {
        array[r,c] = (ran.Next(dim)); // fill it with random numbers.
    }
}

// 2. Create ArrayDataView class in which 
// constructor you pass the array 
// and assign it to DataSource property of DataGrid. 

 dataGrid1.DataSource = new ArrayDataView(array);

or you can try:

var data = new double[5,5]
{
    { 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
    { 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
    { 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
    { 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
};

var rowCount = data.GetLength(0);
var rowLength = data.GetLength(1);

for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex)
{
    var row = new DataGridViewRow();

    for(int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
    {
        row.Cells.Add(new DataGridViewTextBoxCell()
            {
                Value = data[rowIndex, columnIndex]
            });
    }

    dataGrid1.Rows.Add(row);
}

4 Comments

Whats ArrayDataView? Ive never seen this.
It's a custom type which represent view of the multidimensional array data.
Ah right. Seems a bit excessive to use this for such a simple task and doesn't teach the asker how to actually do it.
The first example I admit is a bit overkill for such a simple operation but for complex data perhaps that will suffice. I have edited my answer to provide a simple approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.