如何给DataTable修改里面的列和值

since66 2009-04-14 03:50:15
有个DataTable里面的数据,如图:

列 A B C
行 1 2 3
行 1 2 3
行 1 2 3

我想写个方法
/// <summary>
/// 填充dt
/// </summary>
/// <param name="dt">dt名字</param>
/// <param name="Columns">列名</param>
/// <param name="value">列值</param>
/// <param name="i">第几列</param>
/// <returns></returns>
private DataTable getTB(DataTable dt, string Columns,string value,int i)
{
}

这个方法要求可以添加其他列,例如D,E,f....不定,可以添加行A[3]=5或修改B[0]=10等等,请问怎么写?谢谢!
...全文
656 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
沐雪架构师 2009-09-23
  • 打赏
  • 举报
回复
能来些中文吗? 我们是中国人,时时不忘我们的文字!!
[Quote=引用 2 楼 liuyeede 的回复:]
下面的代码出自msdn:
C# codeprivate System.Data.DataSet dataSet;privatevoid MakeDataTables()
{// Run all of the functions. MakeParentTable();
MakeChildTable();
MakeDataRelation();
BindToDataGrid();
}privatevoid MakeParentTable()
{// Create a new DataTable. System.Data.DataTable table=new DataTable("ParentTable");// Declare variables for DataColumn and DataRow objects. 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";
column.ReadOnly=true;
column.Unique=true;// Add the Column to the DataColumnCollection. table.Columns.Add(column);// Create second column. column=new DataColumn();
column.DataType= System.Type.GetType("System.String");
column.ColumnName="ParentItem";
column.AutoIncrement=false;
column.Caption="ParentItem";
column.ReadOnly=false;
column.Unique=false;// Add the column to the table. table.Columns.Add(column);// Make the ID column the primary key column. DataColumn[] PrimaryKeyColumns=new DataColumn[1];
PrimaryKeyColumns[0]= table.Columns["id"];
table.PrimaryKey= PrimaryKeyColumns;// Instantiate the DataSet variable. dataSet=new DataSet();// Add the new DataTable to the DataSet. dataSet.Tables.Add(table);// Create three new DataRow objects and add// them to the DataTablefor (int i=0; i<=2; i++)
{
row= table.NewRow();
row["id"]= i;
row["ParentItem"]="ParentItem"+ i;
table.Rows.Add(row);
}
}privatevoid MakeChildTable()
{// Create a new DataTable. DataTable table=new DataTable("childTable");
DataColumn column;
DataRow row;// Create first column and add to the DataTable. column=new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName="ChildID";
column.AutoIncrement=true;
column.Caption="ID";
column.ReadOnly=true;
column.Unique=true;// Add the column to the DataColumnCollection. table.Columns.Add(column);// Create second column. column=new DataColumn();
column.DataType= System.Type.GetType("System.String");
column.ColumnName="ChildItem";
column.AutoIncrement=false;
column.Caption="ChildItem";
column.ReadOnly=false;
column.Unique=false;
table.Columns.Add(column);// Create third column. column=new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName="ParentID";
column.AutoIncrement=false;
column.Caption="ParentID";
column.ReadOnly=false;
column.Unique=false;
table.Columns.Add(column);

dataSet.Tables.Add(table);// Create three sets of DataRow objects,// five rows each, and add to DataTable.for(int i=0; i<=4; i++)
{
row= table.NewRow();
row["childID"]= i;
row["ChildItem"]="Item"+ i;
row["ParentID"]=0 ;
table.Rows.Add(row);
}for(int i=0; i<=4; i++)
{
row= table.NewRow();
row["childID"]= i+5;
row["ChildItem"]="Item"+ i;
row["ParentID"]=1 ;
table.Rows.Add(row);
}for(int i=0; i<=4; i++)
{
row= table.NewRow();
row["childID"]= i+10;
row["ChildItem"]="Item"+ i;
row["ParentID"]=2 ;
table.Rows.Add(row);
}
}privatevoid MakeDataRelation()
{// DataRelation requires two DataColumn// (parent and child) and a name. DataColumn parentColumn=
dataSet.Tables["ParentTable"].Columns["id"];
DataColumn childColumn=
dataSet.Tables["ChildTable"].Columns["ParentID"];
DataRelation relation=new
DataRelation("parent2Child", parentColumn, childColumn);
dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
}privatevoid BindToDataGrid()
{// Instruct the DataGrid to bind to the DataSet, with the// ParentTable as the topmost DataTable. dataGrid1.SetDataBinding(dataSet,"ParentTable");
}
[/Quote]
lanxingxing 2009-04-14
  • 打赏
  • 举报
回复
回帖是一种美德!每天回帖即可获得 10 分可用分! 换几个可用分
qq2013 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huwuling922 的回复:]
学习,up
[/Quote]
up
热学沸腾56 2009-04-14
  • 打赏
  • 举报
回复
学习,up
liuyeede 2009-04-14
  • 打赏
  • 举报
回复
下面的代码出自msdn:

private System.Data.DataSet dataSet;

private void MakeDataTables()
{
// Run all of the functions.
MakeParentTable();
MakeChildTable();
MakeDataRelation();
BindToDataGrid();
}

private void MakeParentTable()
{
// Create a new DataTable.
System.Data.DataTable table = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
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";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);

// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);

// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;

// Instantiate the DataSet variable.
dataSet = new DataSet();
// Add the new DataTable to the DataSet.
dataSet.Tables.Add(table);

// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i<= 2; i++)
{
row = table.NewRow();
row["id"] = i;
row["ParentItem"] = "ParentItem " + i;
table.Rows.Add(row);
}
}

private void MakeChildTable()
{
// Create a new DataTable.
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;

// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName = "ChildID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;

// Add the column to the DataColumnCollection.
table.Columns.Add(column);

// Create second column.
column = new DataColumn();
column.DataType= System.Type.GetType("System.String");
column.ColumnName = "ChildItem";
column.AutoIncrement = false;
column.Caption = "ChildItem";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

// Create third column.
column = new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName = "ParentID";
column.AutoIncrement = false;
column.Caption = "ParentID";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

dataSet.Tables.Add(table);

// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 0 ;
table.Rows.Add(row);
}
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i + 5;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 1 ;
table.Rows.Add(row);
}
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i + 10;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 2 ;
table.Rows.Add(row);
}
}

private void MakeDataRelation()
{
// DataRelation requires two DataColumn
// (parent and child) and a name.
DataColumn parentColumn =
dataSet.Tables["ParentTable"].Columns["id"];
DataColumn childColumn =
dataSet.Tables["ChildTable"].Columns["ParentID"];
DataRelation relation = new
DataRelation("parent2Child", parentColumn, childColumn);
dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
}

private void BindToDataGrid()
{
// Instruct the DataGrid to bind to the DataSet, with the
// ParentTable as the topmost DataTable.
dataGrid1.SetDataBinding(dataSet,"ParentTable");
}


charles0525 2009-04-14
  • 打赏
  • 举报
回复
学习一下临时表就会了 !

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧