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

How can I dynamically add colums and rows to an empty dataset?(如何将列和行动态添加到空数据集中?)

本文介绍了如何将列和行动态添加到空数据集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新数据集

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

推荐答案

试着把你的代码改成这个

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

这篇关于如何将列和行动态添加到空数据集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将列和行动态添加到空数据集中?

基础教程推荐