如何动态地将列和行添加到空数据集中?

发布于 2024-11-02 08:21:37 字数 1490 浏览 1 评论 0原文

我创建了一个新的数据集,因为

DataSet local_ds2 = new DataSet();

我尝试动态添加行和列

DataColumn dcAmount = new DataColumn("EmpID");
    local_ds2.Tables["ACHFile"].Columns.Add(dcAmount);

    DataColumn dcName = new DataColumn("Name");
    local_ds2.Tables["ACHFile"].Columns.Add(dcName);

    DataColumn dcBnkRoutingNumber = new DataColumn("BankRoutingNumber");
    local_ds2.Tables["ACHFile"].Columns.Add(dcBnkRoutingNumber);

    DataColumn dcBnkAccount = new DataColumn("BankAccount");
    local_ds2.Tables["ACHFile"].Columns.Add(dcBnkAccount);

    DataColumn dc = new DataColumn("Amount");
    local_ds2.Tables["ACHFile"].Columns.Add(dc);

    DataColumn dc1 = new DataColumn("BankAccountTypeID");
    local_ds2.Tables["ACHFile"].Columns.Add(dc1);

    for (int i = 0; i < chkcnt; i++)
    {
        local_ds2.Tables["ACHFile"].Rows[i]["EmpID"] = EmpID[i];
        local_ds2.Tables["ACHFile"].Rows[i]["Name"] = Empname[i];
        local_ds2.Tables["ACHFile"].Rows[i]["BankRoutingNumber"] = BnkRoutingNumber[i];
        local_ds2.Tables["ACHFile"].Rows[i]["BankAccount"] = BnkAccount[i];
        local_ds2.Tables["ACHFile"].Rows[i]["BankAccountTypeID"] = AchDB.strBankTypeID[i];
        local_ds2.Tables["ACHFile"].Rows[i]["Amount"] = AchDB.Amount1[i];
        if (AchDB.strBankTypeID[i].ToString() == "D")
            strBankAccntType = "BankAccountTypeID='" + AchDB.strBankTypeID[i].ToString() + "'";
    }

,但我无法动态添加列和行,任何人都可以帮助我

I created a new dataset as

DataSet local_ds2 = new DataSet();

I tried this to add rows and columns dynamically

DataColumn dcAmount = new DataColumn("EmpID");
    local_ds2.Tables["ACHFile"].Columns.Add(dcAmount);

    DataColumn dcName = new DataColumn("Name");
    local_ds2.Tables["ACHFile"].Columns.Add(dcName);

    DataColumn dcBnkRoutingNumber = new DataColumn("BankRoutingNumber");
    local_ds2.Tables["ACHFile"].Columns.Add(dcBnkRoutingNumber);

    DataColumn dcBnkAccount = new DataColumn("BankAccount");
    local_ds2.Tables["ACHFile"].Columns.Add(dcBnkAccount);

    DataColumn dc = new DataColumn("Amount");
    local_ds2.Tables["ACHFile"].Columns.Add(dc);

    DataColumn dc1 = new DataColumn("BankAccountTypeID");
    local_ds2.Tables["ACHFile"].Columns.Add(dc1);

    for (int i = 0; i < chkcnt; i++)
    {
        local_ds2.Tables["ACHFile"].Rows[i]["EmpID"] = EmpID[i];
        local_ds2.Tables["ACHFile"].Rows[i]["Name"] = Empname[i];
        local_ds2.Tables["ACHFile"].Rows[i]["BankRoutingNumber"] = BnkRoutingNumber[i];
        local_ds2.Tables["ACHFile"].Rows[i]["BankAccount"] = BnkAccount[i];
        local_ds2.Tables["ACHFile"].Rows[i]["BankAccountTypeID"] = AchDB.strBankTypeID[i];
        local_ds2.Tables["ACHFile"].Rows[i]["Amount"] = AchDB.Amount1[i];
        if (AchDB.strBankTypeID[i].ToString() == "D")
            strBankAccntType = "BankAccountTypeID='" + AchDB.strBankTypeID[i].ToString() + "'";
    }

But I am unable to add columns and rows dynamically can any one help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音栖息无 2024-11-09 08:21:37

尝试将代码更改为此

DataRow dr;        

for (int i = 0; i < chkcnt; i++)
        {
            dr = local_ds2.Tables["ACHFile"].NewRow();
            dr["EmpID"] = EmpID[i];
            dr["Name"] = Empname[i];
            dr["BankRoutingNumber"] = BnkRoutingNumber[i];
            dr["BankAccount"] = BnkAccount[i];
            dr["BankAccountTypeID"] = AchDB.strBankTypeID[i];
            dr["Amount"] = AchDB.Amount1[i];
            if (AchDB.strBankTypeID[i].ToString() == "D")
                strBankAccntType = "BankAccountTypeID='" + AchDB.strBankTypeID[i].ToString() + "'";
            local_ds2.Tables["ACHFile"].Rows.Add(dr);
        }

您没有将行添加到数据表中

Try changing your code to this

DataRow dr;        

for (int i = 0; i < chkcnt; i++)
        {
            dr = local_ds2.Tables["ACHFile"].NewRow();
            dr["EmpID"] = EmpID[i];
            dr["Name"] = Empname[i];
            dr["BankRoutingNumber"] = BnkRoutingNumber[i];
            dr["BankAccount"] = BnkAccount[i];
            dr["BankAccountTypeID"] = AchDB.strBankTypeID[i];
            dr["Amount"] = AchDB.Amount1[i];
            if (AchDB.strBankTypeID[i].ToString() == "D")
                strBankAccntType = "BankAccountTypeID='" + AchDB.strBankTypeID[i].ToString() + "'";
            local_ds2.Tables["ACHFile"].Rows.Add(dr);
        }

You are not adding the rows to the Datatable

灼痛 2024-11-09 08:21:37

应该像...

        DataRow myRow;
        DataTable ACHFile = new DataTable();
        DataColumn EmpID = new DataColumn("EmpID", System.Type.GetType("System.String"));
        ACHFile.Columns.Add(EmpID );

    for (int i = 0; i < chkcnt; i++)
    {
        myRow = ACHFile.NewRow();
        myRow["EmpID"] = EmpID[i];
        ACHFile.Rows.Add(myRow);
.......
    }

should be like...

        DataRow myRow;
        DataTable ACHFile = new DataTable();
        DataColumn EmpID = new DataColumn("EmpID", System.Type.GetType("System.String"));
        ACHFile.Columns.Add(EmpID );

    for (int i = 0; i < chkcnt; i++)
    {
        myRow = ACHFile.NewRow();
        myRow["EmpID"] = EmpID[i];
        ACHFile.Rows.Add(myRow);
.......
    }
国粹 2024-11-09 08:21:37

尝试将数据列放入表行中,然后将表行添加到表中。现在看起来您正在向表中添加一列,跳过行的步骤

try putting the datacolums in a tablerow and then adding the tablerow to the table. Right now it looks like you're adding a column to a table skipping the step of the rows

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文