C# 往Datatable中添加新行的步骤

2021-03-28 02:25

阅读:605


  •             //往Datatable中添加新行的步骤主要有
                //      第一,声明一个Datatable;
                //      第二,向Datatable中添加列;
                //      第三,向Datatable中添加行
                
                //①声明一个dataTable
                DataTable Year = new DataTable();
                //②添加列
                Year.Columns.Add("Value",typeof(int));
                Year.Columns.Add("Name",typeof(string));

                //③添加行
                for (int i = 0; i            {
                    DataRow row = Year.NewRow();
                    int value = yearStart + i;
                    string  name= (yearStart + i).ToString();

                    object[] objs = { value, name};
                    //dr.ItemArray = objs;
                    row["Value"] = objs[0];
                    row["Name"] = objs[1];
                    Year.Rows.Add(row.ItemArray);
                    
                }
                ddlYear.DataSource = Year;
                ddlYear.DataTextField = "Name";
                ddlYear.DataValueField = "Value";
                ddlYear.DataBind();
            }


  • 评论


    亲,登录后才可以留言!