在 Datagrid-WPF 中垂直显示水平表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13954344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:41:27  来源:igfitidea点击:

Display a horizontal table vertically in a Datagrid-WPF

c#sql-serverwpfwpfdatagrid

提问by Indhi

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 my_DataSet.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);
        }
        da.Fill(ds);
        dataGrid1.ItemsSource = ds.Tables[1].DefaultView;
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
   }
}

Thanks

谢谢

回答by Indhi

At first I tried rotating my Table in my DB using Pivot table, but couldn't succeed..

起初我尝试使用数据透视表在我的数据库中旋转我的表,但无法成功..

then I found to set the datagrid.layouttransform and a datagrid.Cellstyle for rotating the view of the table.

然后我发现设置 datagrid.layouttransform 和 datagrid.Cellstyle 来旋转表格的视图。

         <DataGrid.LayoutTransform>
             <TransformGroup>
                 <RotateTransform Angle="90"/>
                  <MatrixTransform Matrix="-1,0,0,1,0,0"/>
              </TransformGroup>
           </DataGrid.LayoutTransform>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <RotateTransform Angle="-90"/>
                                <ScaleTransform ScaleX="1" ScaleY="-1" />
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.ColumnHeaderStyle>
           <DataGrid.CellStyle>
                <Style  TargetType="DataGridCell">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <RotateTransform Angle="-90"/>
                                <ScaleTransform ScaleX="1" ScaleY="-1" />
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>

and set the height and width of the columns accordingly and it works fine. Thanks !

并相应地设置列的高度和宽度,它工作正常。谢谢 !