是否可以在datagridview中切换行和列?

时间:2022-03-24 04:39:54

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

我在DataTable中有10条数据记录,其中包含3个字段“Foo”,“Bar”和“Baz”。

If I connect wire this into a DataGridView I see 10 rows and 3 columns, and the column headers display the names of the fields.

如果我将它连接到DataGridView,我会看到10行和3列,列标题显示字段的名称。

I'm wondering how easy it is to reverse the rows and columns so that with the same data I end up with 3 rows and 10 columns, with field names being displayed in the row headers.

我想知道反转行和列是多么容易,因此使用相同的数据我最终得到3行和10列,字段名称显示在行标题中。


I can do some things manualy, like overriding an OnPaint method and drawing the field names directly into the row header cells, but I'm looking for something more automated.

我可以手工做一些事情,比如重写OnPaint方法并将字段名称直接绘制到行标题单元格中,但我正在寻找更自动化的东西。

Again, there's a suggestion of swapping values manualy, but unless I make all values Strings, this isn't going to work. 3 columns in a data table - Foo, Bar, and Baz of types int, float and string just won't transpose.

同样,有一个建议是手动交换值,但除非我将所有值都设置为字符串,否则这不会起作用。数据表中的3列 - 类型为int,float和string的Foo,Bar和Baz不会转置。

Even if I managed all of these manual changes, my data grids have CheckBox columns, ComboBox columns - no such row counterpart exists - there is no CheckBox row or ComboBox row. Where I currently just need to tell the compiler to "Add a ComboBoxColumn" I'd have to re-write so that every cell was generated individually.

即使我管理了所有这些手动更改,我的数据网格也有CheckBox列,ComboBox列 - 没有这样的行对应存在 - 没有CheckBox行或ComboBox行。我目前只需要告诉编译器“添加一个ComboBoxColumn”,我必须重新编写,以便每个单元格都是单独生成的。

Ideally I'd like a TransposableDataGridView which exposed all functionality of the DataGridView, with an additional bool property "Transposed". That way I could leave all of my code exactly as it is - I wouldn't have to change anything except for the type of the grid.

理想情况下,我想要一个TransposableDataGridView,它暴露了DataGridView的所有功能,并附加了一个bool属性“Transposed”。这样我就可以完全保留我的所有代码 - 除了网格的类型之外,我不需要改变任何东西。

If nothing like this exists, I might just have to go and write it. (Should only take me a year or so! :)

如果不存在这样的情况,我可能只需要去写它。 (应该只花一年时间!:)

5 个解决方案

#1


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.

您可以创建新的DataTable,添加适当数量的collumns,然后将值从一个表复制到另一个表,只需交换行和列。

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;

#2


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

我使用Developer Expres Vertical Grid ..就像你想要一个旋转的网格。它还允许每行使用不同的编辑器。

Link to vertical grid product page

链接到垂直网格产品页面

#3


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.

您可以通过以编程方式添加所需的列数(例如,再生成10个列)来执行此操作,然后以编程方式交换row,col与col,row。

#4


This code should do the trick http://aspalliance.com/538_CodeSnip_Pivot_Tables_with_ADONET_and_Display_in_a_DataGrid_Paged_Horizontally

这段代码应该做的伎俩http://aspalliance.com/538_CodeSnip_Pivot_Tables_with_ADONET_and_Display_in_a_DataGrid_Paged_Horizo​​ntally

The display is asp.net but you can bind the resulting datatable to datagridview and get the result you want

显示是asp.net,但您可以将生成的数据绑定到datagridview并获得所需的结果

#5


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;

}

#1


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.

您可以创建新的DataTable,添加适当数量的collumns,然后将值从一个表复制到另一个表,只需交换行和列。

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;

#2


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

我使用Developer Expres Vertical Grid ..就像你想要一个旋转的网格。它还允许每行使用不同的编辑器。

Link to vertical grid product page

链接到垂直网格产品页面

#3


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.

您可以通过以编程方式添加所需的列数(例如,再生成10个列)来执行此操作,然后以编程方式交换row,col与col,row。

#4


This code should do the trick http://aspalliance.com/538_CodeSnip_Pivot_Tables_with_ADONET_and_Display_in_a_DataGrid_Paged_Horizontally

这段代码应该做的伎俩http://aspalliance.com/538_CodeSnip_Pivot_Tables_with_ADONET_and_Display_in_a_DataGrid_Paged_Horizo​​ntally

The display is asp.net but you can bind the resulting datatable to datagridview and get the result you want

显示是asp.net,但您可以将生成的数据绑定到datagridview并获得所需的结果

#5


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;

}