是否可以在 datagridview 中切换行和列? [英] Is it possible to switch rows and columns in a datagridview?

查看:29
本文介绍了是否可以在 datagridview 中切换行和列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 DataTable 中有 10 条数据记录,其中有 3 个字段Foo"、Bar"和Baz".

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

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

<小时>

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

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

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

理想情况下,我想要一个 TransposableDataGridView,它公开了 DataGridView 的所有功能,并带有一个额外的 bool 属性Transposed".这样我就可以让我的所有代码保持原样——除了网格的类型之外,我不需要改变任何东西.

如果没有这样的东西存在,我可能只需要去写它.(应该只需要我一年左右的时间!:)

解决方案

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

我认为您不能像设置列标题一样设置行标题(或者至少我不知道如何设置),因此您可以将字段名称放在单独的列中.

DataTable oldTable = new DataTable();...数据表 newTable = 新数据表();newTable.Columns.Add("字段名");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 

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, and the column headers display the names of the fields.

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.


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.

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.

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.

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.

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

解决方案

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;

这篇关于是否可以在 datagridview 中切换行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆