新构建一个datatable,怎么构建?一行一行地加入数据?请说下方法?
并把datatable作为数据源绑定到.如.dropdownlist,datagrid等的方法?请大家一定要帮忙.最好很容易懂的那种!!
DataTable dt = new DataTable("Staff");
DataColumn dc;
dc = new DataColumn("ID", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
dc = new DataColumn("BeginDate", System.Type.GetType("System.DateTime"));
dt.Columns.Add(dc);
dc = new DataColumn("EndDate", System.Type.GetType("System.DateTime"));
dt.Columns.Add(dc);
dc = new DataColumn("Company", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("Department", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("Manager", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("Telephone", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["ID"] = 1;
dr["BeginDate"] = System.DateTime.Today;
dr["EndDate"] = System.DateTime.Today;
dr["Company"] = "××××";
dr["Department"] = "××××";
dr["Manager"] = "××";
dr["Telephone"] = "654321";
dt.Rows.Add(dr);
dgWork.DataSource = dt;
dgWork.DataBind();
--------------
DataTable WorkTable = ds.Tables.Add("TableName");//建立表 TableName=表名
for(int i = 0; i < Count; i++)//建表
{
DataColumn myColumn = new DataColumn();
myColumn.DataType = typeof(object);
myColumn.Caption = ""; myColumn.ColumnName = ""
WorkTable.Columns.Add(myColumn);
}
this.dlbF.DataSource = dsSource.Tables[0].DefaultView;
this.dlbF.DataTextField = "";
this.dlbF.DataValueField = "";
this.dlbF.DataBind();
-------------
DataTable tbTest = new DataTable(); // 新建一个DataTable
// 给Table定义添加Column
DataColumn col;
col = new DataColumn("id",typeof(int));
tbTest.Columns.Add(col);
col = new DataColunm("title",typeof(string));
col.MaxLength = 50; // 设置字段长度
tbTest.Columns.Add(col);
// 设置主键
tbTest.PrimaryKey = new DataColumn[]{tbTest.Columns["id"]};
// 添加数据
DataRow row = tbTest.NewRow();
row["id"] = 1;
row["title"] = "第一条记录";
tbTest.Rows.Add(row);
// 设置主键 一些方法会用到主键的
tbTest.PrimaryKey = new DataColumn[]{tbTest.Columns["id"]};