简体   繁体   中英

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:

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 . 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 . 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.

Check out DataSet class and add your data to that. Bind your datagrid views to one dataset and use any combination of dgvs to edit, select, modify, and pass selected values etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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