简体   繁体   English

重用DataGridView

[英]Re-using a DataGridView

I have a custom control with a TabControl and a DataGridView that I've added to a WinForm with the name "DGV" and wrote 2 methods on that control that are like this: 我有一个带有TabControlDataGridView的自定义控件,该TabControl已添加到名称为“ DGV”的WinForm ,并在该控件上编写了两个方法,如下所示:

public void addTabs(string str)
{
    tabControl1.TabPages.Add(str, str);
    tabControl1.TabPages[str].Controls.Add(dataGridView1);
}

public void fillDataGrid(string Name, long Size1, long Size2)
{
    int cols = dataGridView1.Rows.Add();
    DataGridViewRow row = dataGridView1.Rows[cols];
    row.Cells["FileName"].Value = Name;
    row.Cells["Size"].Value = Size1 + " Kb";
    row.Cells["CompressedSize"].Value = Size2 + " Kb";
    row.Cells["Format"].Value = "Zip";
}

So then in my Form I'm doing this: 因此,在我的表格中,我正在这样做:

foreach (FileInfo f in dInfo.GetFiles("*.zip"))
{
    string n = Path.GetFileNameWithoutExtension(f.Name);
    Dgv.addTabs(n);

    string totalPath = Tx.Text + "\\" + f;

    using (ZipFile zip = ZipFile.Read(totalPath))
    {
        foreach (ZipEntry zp in zip)
        {
            Dgv.fillDataGrid(zp.FileName, zp.CompressedSize, zp.UncompressedSize);
        }
    }
}

Doing this only a tabPage will have all the data in the DataGridView . 仅执行tabPage即可将所有数据保存在DataGridView Creating new DataGridViews inside the loop and then add them to a TabControl will do the trick, but the problem is that I want to manage the selected items in the DataGridView cells, so, I need to find a way to add the loop data properly to the corresponding tabPage DataGridView . 在循环内创建新的DataGridViews ,然后将它们添加到TabControl可以解决问题,但是问题是我想管理DataGridView单元格中的选定项,因此,我需要找到一种将循环数据正确添加到的方法。相应的tabPage DataGridView Hope someone can help me to find a way to reuse the DataGridView per tab or finding a better way to do what I want. 希望有人可以帮助我找到一种重新使用每个选项卡的DataGridView的方法,或者找到一种更好的方法来完成我想要的事情。

Check out DataSet class and add your data to that. 签出DataSet类,然后向其中添加数据。 Bind your datagrid views to one dataset and use any combination of dgvs to edit, select, modify, and pass selected values etc 将您的datagrid视图绑定到一个数据集,并使用dgvs的任意组合来编辑,选择,修改和传递所选值等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM