Good day!

There is a task to derive a matrix on a form (two-dimensional array) through C # WinForms. Now I think in what way it is easiest to do. There is a ListBox control in mind, but I don’t know if it will display it correctly.

There is a view of the standard matrix.

    4 answers 4

    In my opinion, the simplest and at the same time, the most functional will be the combination of DataTable with DataGridView.

    Immediately I see a potential problem in that the DataGridView will not automatically add / delete columns at the matrix when the data changes, it will be necessary to implement it manually.

    Nothing like this, with this code:

    DataTable matrix = new DataTable("Матрица"); ... //загоняем матрицу в matrix ... dataGridView.DataSource = matrix; /* этой строчкой теперь все изменения dataGridView будут отражаться в matrix */ 

    And if we add to the form, for example, TextBox and Button to add a new column, the implementation of pressing the button will be as follows:

     matrix.Columns.Add(textBox.Text, typeof(int)); dataGridView.Update(); 

    And the new column will be added to the matrix. Moreover, by virtue of your statement about the functionality, you can save this matrix to a file in one line:

     matrix.WriteXml("Matrix.xml", XmlWriteMode.WriteSchema); 

    And also one line to load if necessary ...

      Especially for matrix data, DevExpress has a PivotGrid component. But it is paid.

      If you are going to restrict yourself to standard Windows Forms components, I recommend either using DataTable or manually implementing IList and ITypedList in your matrix to represent the rows and columns as dynamic sets. Immediately I see a potential problem in that the DataGridView will not automatically add / delete columns at the matrix when the data changes, it will be necessary to implement it manually.

        In Forms there is a ready-made DataGridView component. This is the most suitable option. True, the component is quite powerful and its configuration will require some effort.

          The easiest way to use TextBox is by setting the MultiLine property = True. And then add the rows of the matrix to the Text property.