Is it possible to switch rows and columns in a datagridview?

前端 未结 5 720
夕颜
夕颜 2020-12-03 22:35

I have 10 records of data in a DataTable which has 3 fields \"Foo\", \"Bar\", and \"Baz\".

If I connect wire this into a DataGridView I see 10 rows and 3 columns, an

相关标签:
5条回答
  • 2020-12-03 22:56

    Apply a LayoutTransform to the DataGrid:

    <DataGrid Name = "reverseThis">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Top" Binding="{Binding Path=Top}"/>
            <DataGridTextColumn Header="Middle" Binding="{Binding Path=Middle}"/>
            <DataGridTextColumn Header="Bottom" Binding="{Binding Path=Bottom}"/>
        </DataGrid.Columns>
        <DataGrid.LayoutTransform>
            <TransformGroup>
                <RotateTransform Angle="-90"/>
                <ScaleTransform ScaleX="1" ScaleY="-1" />
            </TransformGroup>
        </DataGrid.LayoutTransform>
    

    You also need to reverse the column headers:

    <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>
            <Setter Property="FontWeight" Value="Bold"></Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    

    and the cells, otherwise they come out mirrored and rotated:

        <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>
    
    </DataGrid>
    
    0 讨论(0)
  • 2020-12-03 23:03

    You can create new DataTable, add appropriate number of collumns and then copy values from one table to the other, just swap rows and colums.

    I don't think you can set row header in the same way you can set column header (or at least I don't know how), so you can put the field names in separate colum.

    DataTable oldTable = new DataTable();
    
    ...
    
    DataTable newTable = new DataTable();
    
    newTable.Columns.Add("Field Name");
    for (int i = 0; i < oldTable.Rows.Count; i++)
        newTable.Columns.Add();
    
    for (int i = 0; i < oldTable.Columns.Count; i++)
    {
        DataRow newRow = newTable.NewRow();
    
        newRow[0] = oldTable.Columns[i].Caption;
        for (int j = 0; j < oldTable.Rows.Count; j++)
            newRow[j+1] = oldTable.Rows[j][i];
        newTable.Rows.Add(newRow);
    }
    
    dataGridView.DataSource = newTable;
    
    0 讨论(0)
  • 2020-12-03 23:10

    I use the Developer Expres Vertical Grid.. It is like you want a rotated grid. It also allows for a different editor per row.

    Link to vertical grid product page

    0 讨论(0)
  • 2020-12-03 23:11

    You can do this by programmatically adding the number of columns you need (e.g., 7 more to make 10) then programmatically swapping row, col with col, row.

    0 讨论(0)
  • 2020-12-03 23:14

    In my case needed also to add a name to all columns of the new table. I wrote a function to generate a transposed table like this:

    static public DataTable Transpose(DataTable inputTable, List<string> newColumnNames)
    {
        DataTable outputTable = new DataTable();
    
        ///You should also verify if newColumnsNames.Count matches inputTable number of row
    
        //Creates the columns, using the provided column names.
        foreach (var newColumnName in newColumnNames)
        {
            outputTable.Columns.Add(newColumnName, typeof(string));
        }
    
        foreach (DataColumn inputColumn in inputTable.Columns)
        {
            //For each old column we generate a row in the new table
            DataRow newRow = outputTable.NewRow();
    
            //Looks in the former header row to fill in the first column
            newRow[0] = inputColumn.ColumnName.ToString();
    
            int counter = 1;
            foreach (DataRow row in inputTable.Rows)
            {
                newRow[counter] = row[inputColumn.ColumnName].ToString();
                counter++;
            }
            outputTable.Rows.Add(newRow);
        }
    
        return outputTable;
    
    }
    
    0 讨论(0)
提交回复
热议问题