【问题标题】:How to create Datagridview in tab in loop in C#如何在 C# 循环中的选项卡中创建 Datagridview
【发布时间】:2018-10-29 12:14:35
【问题描述】:

我正在开发项目,我必须读取多个 CSV 文件并在 DataGrid 中查看。现在这里的挑战是如果有 5 个文件,我必须创建 5 个不同的选项卡才能在 Datagrid 中查看,这是我无法做到的。这是我的代码:

       DataSet ds = new DataSet();
        foreach (String file in openFileDialogCSVFilePath.FileNames)
        {
            string fileLocation = file;
            string fileName = Path.GetFileName(@fileLocation);
            CsvReader reader = new CsvReader(fileLocation);
            ds = reader.RowEnumerator;
        }
        dGridCSVdata.DataSource = ds;
        dGridCSVdata.DataMember = "The Data";

谁能帮助我如何做到这一点?提前致谢。

更新:

我想以语法方式创建 Tab。

【问题讨论】:

  • @Anas 我已经试过了,在我的情况下没有用
  • 因此您必须以编程方式创建标签页,并根据可用文件总数在每个标签中添加 DatagridView。
  • @SH7 没错。这就是我要找的。​​span>
  • 创建 5 个不同的选项卡以在 Datagrid 中查看,我无法做到这一点 - 你没有告诉我们什么是/不工作。请注意,您对隐藏标签页执行的操作不会自动更新!

标签: c# datagridview dataset


【解决方案1】:

这是一个代码 sn-p 。所以我有一个 TabControl tabControl1with One TabPage。

步骤1:读取并获取文件总数并在List<string>

中存储文件路径

步骤2:基于文件计数生成TabPage

步骤3:读取CSV文件并将其存储在Datatable

第4步:对于TabPage添加DataGridViewDataSource 987654328 @

请注意,这未测试。

private void frmMain_Load(object sender, EventArgs e)
    {
        //Get Total Number of File to create Total Tab
        //Pass the count in the for loop
        //Store the File Path of Each File in List<string> lstFilePath

        for (int i = 0; i < 5; i++) //i< lstFilePath.Count
        {
            //Create Tab Programatically
            this.tabControl1.TabPages.Add("Tab Page"+ (i+1).ToString());                

            //Create DataTable to read and Store the CSV File Data
            DataTable Dt = new DataTable();

            //Based on the i value get the File Path from lstFilePath 
            //and pass it to Function below

            Dt = ConvertCSVtoDataTable("");               
            DataGridView grid = new DataGridView();
            this.tabControl1.TabPages[i].Controls.Add(grid);
            grid.DataSource = Dt;
        }
    }

    public static DataTable ConvertCSVtoDataTable(string strFilePath)
    {
        DataTable dt = new DataTable();
        using (StreamReader sr = new StreamReader(strFilePath))
        {
            string[] headers = sr.ReadLine().Split(',');
            foreach (string header in headers)
            {
                dt.Columns.Add(header);
            }
            while (!sr.EndOfStream)
            {
                string[] rows = sr.ReadLine().Split(',');
                DataRow dr = dt.NewRow();
                for (int i = 0; i < headers.Length; i++)
                {
                    dr[i] = rows[i];
                }
                dt.Rows.Add(dr);
            }

        }

        return dt;
    }

【讨论】:

  • 感谢您的回复。你能帮助我在这里tabControl1 span>
  • 已经编辑了答案,也提到了步骤。未测试代码,但我猜您可以使用我在以编程方式添加DataGridViewDataGridViewDataTableDataTable 987654334 @。 span>
猜你喜欢
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 2018-12-28
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多