如何在asp.net中的for循环中创建动态数据行

时间:2015-07-20 06:27:55

标签: c# asp.net

我必须在for循环中创建数据行,每行都有唯一的名称。

for(int;i=0;i<12;i++)
{

Datarow i=new Datarow();
}

是否可以在asp.net中使用。我必须创建12个具有唯一名称

的数据行

3 个答案:

答案 0 :(得分:1)

尝试这样做。希望这对你有用。

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);
    }

快乐编码:)

答案 1 :(得分:0)

您可以使用Distinct关键字创建具有不同键的集合,例如:

List<string> ages = new List<string> { "twentyOne", "FortySix", "FortySix", "FiftyFive", "SevenTeen", "twentyOne", "FiftyFive", "FiftyFive" };
List<int> realAges = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };  

ages = ages.Distinct();
DataTable table = some table you use
DataRow row = table.NewRow();

 for (int i = 0;i < ages.Count(); ++i)
 { 
     if (realAges.Count() < i)
     {
        row[ages[i]] = realAges[i]
     }
 }

答案 2 :(得分:0)

        // Declare a DataTable
        DataTable dtNames = new DataTable("dtNames");
        // Add column to the DataTable
        dtNames.Columns.Add("Name");

        DataRow drName = null;
        // Loop to add new rows with unique names
        for (int i = 1; i < 10; i++ )
        {
            drName = dtNames.NewRow();
            drName["Name"] = "Name0" + i;

            dtNames.Rows.Add(drName);
        }