Answer the question
In order to leave comments, you need to log in
How to get DataGridView values from all forms?
Hello. Help solve the problem, and it's next.
There is a parent form with a button, in the parent form there is a TreeView element in which the user adds an element (the tree is simple, there is no nesting, only the root and elements below), when you click on the added TreeView element, an MDI form opens with a DataGridView in which the user specifies the values, after that when you click on the button in the parent form, you need to go through the tree and collect the values that the user entered from all forms for further calculations. Walking through the tree is not difficult, you can also walk through the forms, but I can get the name of the form, I don’t understand how I can collect the data .. Maybe there are ideas on how to do it more competently.
Answer the question
In order to leave comments, you need to log in
Suppose we need to process a set of some data represented by a table. Each table has a title, the user can add an arbitrary number of tables and edit the data in them. Let's take a simple sum of numbers as an example of processing.
We start by describing the "model" - everything that relates to data and its processing. There are only two classes - SomeClass , which represents the data from the table:
public class SomeClass
{
public int M { get { return Data.GetLength(1); } }
public int N { get { return Data.GetLength(0); } }
public double[,] Data { get; }
public SomeClass(double[,] data)
{
Data = new double[data.GetLength(0), data.GetLength(1)];
Array.Copy(data, Data, data.Length);
}
public double Sum()
{
var s = 0.0;
foreach (var x in Data)
s += x;
return s;
}
}
public class Item
{
public string Title { get; }
public SomeClass Data { get; }
public Item(string title, SomeClass sc)
{
Title = title;
if (sc != null)
Data = new SomeClass(sc.Data);
}
public override string ToString() => Title;
}
public void Fill(Item item)
{
label1.Text = item.Title;
if (item.Data != null)
{
var n = item.Data.N;
var m = item.Data.M;
textBox1.Text = n.ToString();
textBox2.Text = m.ToString();
dataGridView1.ColumnCount = m;
dataGridView1.RowCount = n;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
dataGridView1[j, i].Value = item.Data.Data[i, j];
}
else
{
textBox1.Text = "";
textBox2.Text = "";
dataGridView1.ColumnCount = 0;
dataGridView1.RowCount = 0;
}
}
public Item Extract()
{
string title = label1.Text;
if (string.IsNullOrWhiteSpace(title))
return null;
var n = dataGridView1.RowCount;
var m = dataGridView1.ColumnCount;
if (n == 0 || m == 0)
return new Item(title, null);
var data = new double[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
data[i, j] = Convert.ToDouble(dataGridView1[j, i].Value);
return new Item(title, new SomeClass(data));
}
ItemView iview;
BindingSource bs = new BindingSource()
{ DataSource = typeof(Item) };
public Form1()
{
InitializeComponent();
listBox1.DataSource = bs;
bs.PositionChanged += PositionChanged;
}
private void SaveCurrent()
{
var pr = iview.Extract();
if (pr != null)
{
var old = bs.List.OfType<Item>().First(v => v.Title == pr.Title);
var ind = bs.List.IndexOf(old);
bs[ind] = pr;
}
}
private void PositionChanged(object sender, EventArgs e)
{
if (bs.Current is Item item && bs.Count > 1)
{
SaveCurrent();
iview.Fill(item);
}
}
SaveCurrent();
var items = bs.OfType<Item>().Select(item => item.Data?.Sum() ?? -1);
resLbl.Text = string.Join(";", items);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question