winforms 从另一窗体中的DataGridView获取单元格计数值

t1rydlwq  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(128)

我试图从另一个Form中的DataGridView获取单元格计数值,我有Form1和Form2,在Form2中有一个DataGridView(dgResult),其接收来自01到25之间的DB的15个随机数,其中我对单元格之间的重复数进行计数,重复数由01到25的每个数的标签接收,这都在Form2中,我想在表格1中显示这些标签和计数结果,有人能帮助我吗?我有一门课,从数据库中获取数字:

public int concurso { get; set; }
    public int _01 { get; set; }
    public int _02 { get; set; }
    public int _03 { get; set; }
public int _15 { get; set; }
//Class AddResult
public static DataTable ObterNumeros()
        {
            DataTable tabela = new DataTable();
            DALConexao conexao = new DALConexao(DadosDaConexao.StringDaConexao);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conexao.ObjetConexao;
            SqlDataAdapter da = new SqlDataAdapter("Select * from TBLotofacil ORDER BY Concurso DESC", conexao.StringConexao);
            da.Fill(tabela);
            return tabela;
        }

创建网格的标题Form2

private void AtualizarInformacoesDoGrid()
    {
        //personalizaGrid();
        dgResultadoGeral.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dgResultadoGeral.DataSource = AddResultado.ObterNumeros();
            if (dgResultadoGeral.Rows.Count != 0)
            {
                dgResultadoGeral.Columns[0].HeaderText = "Concurso";
                dgResultadoGeral.Columns[0].Width = 97;
                dgResultadoGeral.Columns[1].HeaderText = "01";
                dgResultadoGeral.Columns[1].Width = 50;
dgResultadoGeral.Columns[15].HeaderText = "15";
                dgResultadoGeral.Columns[15].Width = 50;

...获取细胞计数结果Form2

public void calculaTotalGrid()
        {
            int outVal = 0;
            ResultadoNumeros = dgResultadoGeral.Rows.Cast<DataGridViewRow>()
                .SelectMany(r => r.Cells.Cast<DataGridViewCell>()
                .Where(c => c.ColumnIndex > 0 && int.TryParse(c.Value?.ToString(), out outVal)))
                .GroupBy(c => int.Parse(c.Value.ToString()))
                .ToDictionary(x => x.Key, x => x.Count());

            int count1 = ResultadoNumeros[01];
            lbl_01.Text = count1.ToString();
//fron 01 to 25 
            int count25 = ResultadoNumeros[25];
            lbl_25.Text = count25.ToString();
        }
t5zmwmid

t5zmwmid1#

举个例子。

    • form2**

DataGridView

public class form2 : Form
{
    private Dictionary<int, int> _resultadoNumeros;

    public form2()
    {
        InitializeComponent();
            
        // ...etc.
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // Load the data and populate the DataGridView then call...
        DisplayTheResults();
    }

    internal Dictionary<int, int> RresultadoNumeros
    {
        get
        {
            if (_resultadoNumeros == null)
            {
                int outVal = 0;
                _resultadoNumeros = dgResultadoGeral.Rows.Cast<DataGridViewRow>()
                    .SelectMany(r => r.Cells.Cast<DataGridViewCell>()
                    .Where(c => c.ColumnIndex > 0 && int.TryParse(c.Value?.ToString(), out outVal)))
                    .GroupBy(c => int.Parse(c.Value.ToString()))
                    .ToDictionary(x => x.Key, x => x.Count());
            }

            return _resultadoNumeros;
        }
    }

    private void DisplayTheResults()
    {
        lbl_01.Text = RresultadoNumeros[1].ToString();
        lbl_02.Text = RresultadoNumeros[2].ToString();
        lbl_03.Text = RresultadoNumeros[3].ToString();
        //...
        lbl_25.Text = RresultadoNumeros[25].ToString();
    }
}

注意,您可以利用Tag属性将标签Map到字典并在循环中填充标签。在每个窗体(form1form2)的设计器中,选择lbl_01并按F4,在“属性”窗口中找到Tag属性并分配1。对其他25个标签执行相同的操作,并分配相应的值23,... 25DisplayTheResults方法现在应该看起来像这样:

private void DisplayTheResults()
{
    // assuming the Form is the parent of the labels.
    foreach (var lbl in Controls.OfType<Label>())
    {
        int outKey;

        if (int.TryParse(lbl.Tag?.ToString(), out outKey) &&
            RresultadoNumeros.ContainsKey(outKey))
        {
            lbl.Text = RresultadoNumeros[outKey].ToString();
        }
    }
}

如果标签由不同的容器托管,则使用递归方法来获取它们:

private void DisplayTheResults()
{
    foreach (var lbl in GetChildren(this).OfType<Label>())
    {
        int outKey;

        if (int.TryParse(lbl.Tag?.ToString(), out outKey) &&
            RresultadoNumeros.ContainsKey(outKey))
        {
            lbl.Text = RresultadoNumeros[outKey].ToString();
        }
    }
}

private IEnumerable<Control> GetChildren(Control parent)
{
    var controls = parent.Controls.Cast<Control>();
    return controls.SelectMany(c => GetChildren(c)).Concat(controls);
}
    • form1**
public class form1 : Form
{
    public form1()
    {
        InitializeComponent();

        // ...etc.
    }

    private void WhereYouCreateAndShowForm2()
    {
        using (var frm = new form2())
        {
            frm.ShowDialog();
            DisplayTheResults(frm.RresultadoNumeros);
        }
    }

    private void DisplayTheResults(Dictionary<int, int> dict)
    {
        foreach (var lbl in Controls.OfType<Label>())
        {
            int outKey;

            if (int.TryParse(lbl.Tag?.ToString(), out outKey) &&
                dict.ContainsKey(outKey))
            {
                lbl.Text = dict[outKey].ToString();
            }
        }
    }
}

现在,如果您需要从form2显示from1,那么通过参数化构造函数将字典传递给form1

    • form1**
public class form1 : Form
{
    public form1()
    {
        InitializeComponent();

        // ...etc.
    }

    public form1(Dictionary<int, int> dict) : this()
    {
        DisplayTheResults(dict);
    }        

    private void DisplayTheResults(Dictionary<int, int> dict)
    {
        foreach (var lbl in Controls.OfType<Label>())
        {
            int outKey;

            if (int.TryParse(lbl.Tag?.ToString(), out outKey) &&
                dict.ContainsKey(outKey))
            {
                lbl.Text = dict[outKey].ToString();
            }
        }
    }
}
    • form2**
public class form2 : Form
{
    private Dictionary<int, int> _resultadoNumeros;

    public form2()
    {
        InitializeComponent();
            
        // ...etc.
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // Load the data and populate the DataGridView then call...
        DisplayTheResults();
    }

    internal Dictionary<int, int> RresultadoNumeros
    {
        get
        {
            if (_resultadoNumeros == null)
            {
                int outVal = 0;
                _resultadoNumeros = dgResultadoGeral.Rows.Cast<DataGridViewRow>()
                    .SelectMany(r => r.Cells.Cast<DataGridViewCell>()
                    .Where(c => c.ColumnIndex > 0 && int.TryParse(c.Value?.ToString(), out outVal)))
                    .GroupBy(c => int.Parse(c.Value.ToString()))
                    .ToDictionary(x => x.Key, x => x.Count());
            }

            return _resultadoNumeros;
        }
    }

    private void DisplayTheResults()
    {
        foreach (var lbl in Controls.OfType<Label>())
        {
            int outKey;

            if (int.TryParse(lbl.Tag?.ToString(), out outKey) &&
                RresultadoNumeros.ContainsKey(outKey))
            {
                lbl.Text = RresultadoNumeros[outKey].ToString();
            }
        }
    }

    private void WhereYouCreateAndShowForm1()
    {
        using (var frm = new form1(RresultadoNumeros))
        {
            frm.ShowDialog();
            // ...
        }
    }
}

相关问题