如何在Datagrid-WPF中垂直显示水平表 [英] how to display a horizontal table vertically in a Datagrid-WPF
本文介绍了如何在Datagrid-WPF中垂直显示水平表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从数据库显示的表格。
我想显示水平标题栏 - >垂直
我的桌子结构是
服务器|角色|状态|日期
但是我希望显示为
服务器|
角色|
状态|
日期|
我试图翻转数据集,并尝试构建它。最初构建成功但我无法在我的数据网格上查看任何数据。
亲切的帮助,有没有其他方法来解决这个问题?
这里是我的代码片段
I have a table displayed from a database.
I would like to display the horizontal Header Column-> Vertically
My table Structure is
Server|Role|Status|Date
but I would like to display as
Server|
Role|
Status|
Date|
I tried the to flip the dataset, and tried to build it. Initially the build was successful but I can''t view any data on my datagrid.
kindly help , is there any other way to approach this problem ?
here is my code snippet
SqlConnection con;
SqlDataAdapter da = null;
DataSet ds = null;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
try
{
da = new SqlDataAdapter("Select * from [ServerDB_Test].[dbo].[ServerStatus] ", con);
ds = new DataSet();
foreach (DataTable dt in ds.Tables)
{
DataTable table = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{ table.Columns.Add(Convert.ToString(i)); }
DataRow r;
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
{ r[j] = dt.Rows[j - 1][k]; }
table.Rows.Add(r);
}
ds.Tables.Add(table);
}
dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
谢谢
Thanks
推荐答案
您需要做的是 pivot
您对列内容的数据。这个文章 [ ^ ]可能有用
What you need to do is pivot
your data on a columns content. This article [^]may be useful
这篇关于如何在Datagrid-WPF中垂直显示水平表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文