/定义一个Table
DataTable dt=new DataTable("yeji");
DataRow dr;
DataColumn dc;
//添加第0列
dc=new DataColumn("店名",System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//第1列
dc=new DataColumn("店长名",System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//添加第2列
dc=new DataColumn("总业绩",System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//添加第3列
dc=new DataColumn("租合同数",System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//添加第4列
dc=new DataColumn("售合同数",System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//设置关键字段
DataColumn[] dckey=new DataColumn[]{dt.Columns["部门"]};
dt.PrimaryKey=dckey;
如何将数据添加到自定义的DataTable中?
dt.Rows[行数]["店名"] = "值";
DataTable dt = new DataTable();
dt.Columns.Add("FID", typeof(string));
DataRow dr = dt.NewRow();
dr["FID"] = "123";
dt.Rows.Add(dr);
DataRow r = dt.NewRow();
r["列名"] = "xxx";
dt.Rows.Add(r);
//定义一个Table
DataTable dt=new DataTable("yeji");
DataRow dr;
DataColumn dc;
//添加第0列
dc=new DataColumn("店名",System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//第1列
dc=new DataColumn("店长名",System.Type.GetType("System.String"));
dt.Columns.Add(dc);
//添加第2列
dc=new DataColumn("总业绩",System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//添加第3列
dc=new DataColumn("租合同数",System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//添加第4列
dc=new DataColumn("售合同数",System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
//设置关键字段
DataColumn[] dckey=new DataColumn[]{dt.Columns["部门"]};
dt.PrimaryKey=dckey;
如何将数据添加到自定义的DataTable中?
回复:如何将数据添加到自定义的DataTable中?
顶
回复:如何将数据添加到自定义的DataTable中?
不用那么麻烦的
DataTable dt = new DataTable("yeji");
dt.Columns.Add("部门", typeof(System.String));
dt.Columns.Add("店名", typeof(System.String));
dt.Columns.Add("店长名", typeof(System.String));
dt.Columns.Add("总业绩", typeof(System.Int32));
dt.Columns.Add("租合同数", typeof(System.Int32));
dt.Columns.Add("售合同数", typeof(System.Int32));
DataColumn[] dckey = new DataColumn[] { dt.Columns["部门"] };
dt.PrimaryKey = dckey;
DataRow dr = dt.NewRow();
dr[0] = "餐饮部";
dr[1] = "店1";
dr[2] = "店长";
dr[3] = 1000;
dr[4] = 2000;
dr[5] = 3000;
dt.Rows.Add(dr);
上面是添加了一行数据,如果多条,可以循环加入
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Value");
DataRow dr;
dr = dt.NewRow();
dr["Name"] = "Ntext2";
dr["Value"] = "TesrNtext2";
dt.Rows.Add(dr);
{
// Create new DataTable.
DataTable table = new DataTable("table");
// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "item";
table.Columns.Add(column);
// Create new DataRow objects and add to DataTable.
for(int i = 0; i < 10; i++)
{
row = table.NewRow();
row["id"] = i;
row["item"] = "item " + i;
table.Rows.Add(row);
}
// Set to DataGrid.DataSource property to the table.
dataGrid1.DataSource = table;
}
{
// Duplicate the table and add it to a DataSet
DataSet dsTmp = new DataSet();
DataTable dtTmp = dt.Copy();
dsTmp.Tables.Add(dtTmp);
}
将DataView保存到DataSet中:void DataViewToDataSet(DataView dv)
{
// Clone the structure of the table behind the view
DataTable dtTemp = dv.Table.Clone();
dtTemp.TableName = "Row";
// Populate the table with rows in the view
foreach(DataRowView drv in dv)
dtTemp.ImportRow(drv.Row);
DataSet dsTemp = new DataSet(dv.Table.TableName);
// Add the new table to a DataSet
dsTemp.Tables.Add(dtTemp);
}