簡體   English   中英

您如何根據列值從 excel 電子表格中 select 行並將其插入 dataGridViews?

[英]How do you select rows from an excel spreadsheet based off of a column value and insert it into dataGridViews?

我正在創建一個 Winform,它有一個帶有三個選項卡的選項卡控件,每個選項卡都包含自己的 dataGridViews。 我有一個 excel 電子表格,其中包含所有三個表的數據。 這些都具有相同的標題(名稱、類型、日期、成本)。 type分為三種不同的類別,分別是雇佣、搬遷和服務。 我想創建一個方法,該方法基於某種 where 子句將文件中的數據插入到三個 dataGridViews,該子句根據它們的類型將它們分開。

這是我到目前為止所擁有的。 我添加了一些評論來幫助解決我的問題:

    if (System.IO.File.Exists(@"C:\temp\Activity.csv"))
    {
        string[] lines = File.ReadAllLines(path + file);
        string[] data;

        for (int i = 0; i < lines.Length; i++)
        {
            data = lines[i].ToString().Split(',');

            string[] row = new string[data.Length];

            for (int j = 0; j < data.Length; j++)
            {
                row[j] = data[j].Trim();
            }
            
            //dgv1 is the dataGridView for hire
            form.dgv1.Rows.Add(row); //I want to add the excel row to this datagrid if the 'type' column = hire

            //dgv2 is the dataGridView for service
            form.dgv2.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = service

            //dgv3 is the dataGridView for relocate
            form.dgv3.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = relocate
        }
    }

以下是文件外觀的示例: | 姓名 | 類型 | 日期 | 成本 | | ----- | -------- | ---------- | ---- | | 傑克 | 服務 | 2021 年 3 月 18 日 | 499 | | 約翰 | 租用 | 23/02/2021 | 199 | | 史蒂夫 | 租用 | 2020 年 1 月 11 日 | 249 | | 蘇西 | 搬遷 | 21/03/2021 | 44 |

它會做這樣的事情:

            if (Type = hire)
            {
                dgv1.Rows.Add(row);
            }
            if (Type = service) {
                dgv2.Rows.Add(row);
            }

            if (Type = relocate)
            {
                dgv3.Rows.Add(row);
            }

但我不確定如何編碼

我不確定你上面的代碼是什么工作,但是如果你的row變量從你的列中填充了正確的值,那么你應該能夠檢查你的row數組中第二項的值來獲得類型。 row數組中的第二項應與類型相對應。

所以你應該能夠做這樣的事情:

if (System.IO.File.Exists(@"C:\temp\Activity.csv"))
{
    string[] lines = File.ReadAllLines(path + file);
    string[] data;

    for (int i = 0; i < lines.Length; i++)
    {
        data = lines[i].ToString().Split(',');

        string[] row = new string[data.Length];

        for (int j = 0; j < data.Length; j++)
        {
            row[j] = data[j].Trim();
        }

        //dgv1 is the dataGridView for hire
        if (row[1] == "hire")
        {
            form.dgv1.Rows.Add(row); //I want to add the excel row to this datagrid if the 'type' column = hire
        }
        //dgv2 is the dataGridView for service
        else if (row[1] == "service")
        {
            form.dgv2.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = service
        }
        //dgv3 is the dataGridView for relocate
        else if (row[1] == "relocate")
        {
            form.dgv3.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = relocate
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM