能否取出datatable中某列,将其变成另外一个datatable呢?

蔚蓝理想 2012-04-04 02:16:33
在数据库中按条件查询得到一个具有多列的表,是否有什么方法,将这个表按照列拆分为多个表。

datatable的select()方法只能设置过滤字符,能不设置字符而选择某个列吗?

...全文
135 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yujiexiaoyu 2012-04-04
  • 打赏
  • 举报
回复
dt.DefaultView.ToTable(false,"列名");
蔡袅 2012-04-04
  • 打赏
  • 举报
回复
private static void DemonstrateDataView()
{
// Create a DataTable with three columns.
DataTable table = new DataTable("NewTable");
Console.WriteLine("Original table name: " + table.TableName);
DataColumn column = new DataColumn("ID", typeof(System.Int32));
table.Columns.Add(column);

column = new DataColumn("Category", typeof(System.String));
table.Columns.Add(column);

column = new DataColumn("Product", typeof(System.String));
table.Columns.Add(column);

column = new DataColumn("QuantityInStock", typeof(System.Int32));
table.Columns.Add(column);

// Add some items.
DataRow row = table.NewRow();
row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
table.Rows.Add(row);

row = table.NewRow();
row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
table.Rows.Add(row);

row = table.NewRow();
row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
table.Rows.Add(row);

row = table.NewRow();
row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
table.Rows.Add(row);

row = table.NewRow();
row.ItemArray = new object[] { 5, "Fish", "Salmon", 15 };
table.Rows.Add(row);

row = table.NewRow();
row.ItemArray = new object[] { 6, "Bread", "Croissant", 23};
table.Rows.Add(row);

// Mark all rows as "accepted". Not required
// for this particular example.
table.AcceptChanges();

// Print current table values.
PrintTableOrView(table, "Current Values in Table");

DataView view = new DataView(table);
view.Sort = "Category";
PrintTableOrView(view, "Current Values in View");

DataTable newTable = view.ToTable(true, "Category", "QuantityInStock");
PrintTableOrView(newTable, "Table created from sorted DataView");
Console.WriteLine("New table name: " + newTable.TableName);

Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}

private static void PrintTableOrView(DataView dv, string label)
{
System.IO.StringWriter sw;
string output;
DataTable table = dv.Table;

Console.WriteLine(label);

// Loop through each row in the view.
foreach (DataRowView rowView in dv)
{
sw = new System.IO.StringWriter();

// Loop through each column.
foreach (DataColumn col in table.Columns)
{
// Output the value of each column's data.
sw.Write(rowView[col.ColumnName].ToString() + ", ");
}
output = sw.ToString();
// Trim off the trailing ", ", so the output looks correct.
if (output.Length > 2)
{
output = output.Substring(0, output.Length - 2);
}
// Display the row in the console window.
Console.WriteLine(output);
}
Console.WriteLine();
}

private static void PrintTableOrView(DataTable table, string label)
{
System.IO.StringWriter sw;
string output;

Console.WriteLine(label);

// Loop through each row in the table.
foreach (DataRow row in table.Rows)
{
sw = new System.IO.StringWriter();
// Loop through each column.
foreach (DataColumn col in table.Columns)
{
// Output the value of each column's data.
sw.Write(row[col].ToString() + ", ");
}
output = sw.ToString();
// Trim off the trailing ", ", so the output looks correct.
if (output.Length > 2)
{
output = output.Substring(0, output.Length - 2);
}
// Display the row in the console window.
Console.WriteLine(output);
} //
Console.WriteLine();
}




该示例在控制台窗口中显示以下输出:
Original table name: NewTable
Current Values in Table
1, Fruit, Apple, 14
2, Fruit, Orange, 27
3, Bread, Muffin, 23
4, Fish, Salmon, 12
5, Fish, Salmon, 15
6, Bread, Croissant, 23

Current Values in View
3, Bread, Muffin, 23
6, Bread, Croissant, 23
4, Fish, Salmon, 12
5, Fish, Salmon, 15
1, Fruit, Apple, 14
2, Fruit, Orange, 27

Table created from sorted DataView
Bread, 23
Fish, 12
Fish, 15
Fruit, 14
Fruit, 27

New table name: NewTable

蔚蓝理想 2012-04-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

可以通过默认视图来转换,
dt.DefaultView.ToTable(false,"列名2");
[/Quote]

亲测,可实现!谢谢高手!
蔚蓝理想 2012-04-04
  • 打赏
  • 举报
回复
转换后的表名字是?我在研究一下啊!
[Quote=引用 3 楼 的回复:]

可以通过默认视图来转换,
dt.DefaultView.ToTable(false,"列名2");
[/Quote]
dalmeeme 2012-04-04
  • 打赏
  • 举报
回复
可以通过默认视图来转换,
dt.DefaultView.ToTable(false,"列名2");
bdmh 2012-04-04
  • 打赏
  • 举报
回复
自己写代码转换
fangpengyu 2012-04-04
  • 打赏
  • 举报
回复
不能,没有这种直接的方法

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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