How to add New Column with Value to the Existing DataTable?

后端 未结 3 1016
轻奢々
轻奢々 2020-12-23 15:41

I have One DataTable with 5 Columns and 10 Rows. Now I want to add one New Column to the DataTable and I want to assign DropDownList value to the New Column. So th

相关标签:
3条回答
  • 2020-12-23 16:29

    Add the column and update all rows in the DataTable, for example:

    DataTable tbl = new DataTable();
    tbl.Columns.Add(new DataColumn("ID", typeof(Int32)));
    tbl.Columns.Add(new DataColumn("Name", typeof(string)));
    for (Int32 i = 1; i <= 10; i++) {
        DataRow row = tbl.NewRow();
        row["ID"] = i;
        row["Name"] = i + ". row";
        tbl.Rows.Add(row);
    }
    DataColumn newCol = new DataColumn("NewColumn", typeof(string));
    newCol.AllowDBNull = true;
    tbl.Columns.Add(newCol);
    foreach (DataRow row in tbl.Rows) {
        row["NewColumn"] = "You DropDownList value";
    }
    //if you don't want to allow null-values'
    newCol.AllowDBNull = false;
    
    0 讨论(0)
  • 2020-12-23 16:35
    //Data Table
    
     protected DataTable tblDynamic
            {
                get
                {
                    return (DataTable)ViewState["tblDynamic"];
                }
                set
                {
                    ViewState["tblDynamic"] = value;
                }
            }
    //DynamicReport_GetUserType() function for getting data from DB
    
    
    System.Data.DataSet ds = manage.DynamicReport_GetUserType();
                    tblDynamic = ds.Tables[13];
    
    //Add Column as "TypeName"
    
                    tblDynamic.Columns.Add(new DataColumn("TypeName", typeof(string)));
    
    //fill column data against ds.Tables[13]
    
    
                    for (int i = 0; i < tblDynamic.Rows.Count; i++)
                    {
    
                        if (tblDynamic.Rows[i]["Type"].ToString()=="A")
                        {
                            tblDynamic.Rows[i]["TypeName"] = "Apple";
                        }
                        if (tblDynamic.Rows[i]["Type"].ToString() == "B")
                        {
                            tblDynamic.Rows[i]["TypeName"] = "Ball";
                        }
                        if (tblDynamic.Rows[i]["Type"].ToString() == "C")
                        {
                            tblDynamic.Rows[i]["TypeName"] = "Cat";
                        }
                        if (tblDynamic.Rows[i]["Type"].ToString() == "D")
                        {
                            tblDynamic.Rows[i]["TypeName"] = "Dog;
                        }
                    }
    
    0 讨论(0)
  • 2020-12-23 16:38

    Without For loop:

    Dim newColumn As New Data.DataColumn("Foo", GetType(System.String))     
    newColumn.DefaultValue = "Your DropDownList value" 
    table.Columns.Add(newColumn) 
    

    C#:

    System.Data.DataColumn newColumn = new System.Data.DataColumn("Foo", typeof(System.String));
    newColumn.DefaultValue = "Your DropDownList value";
    table.Columns.Add(newColumn);
    
    0 讨论(0)
提交回复
热议问题