是否有可能在切换一个DataGridView的行和列? [英] Is it possible to switch rows and columns in a datagridview?

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

问题描述

我有10条记录在数据表里面有3个字段富,酒吧和巴兹。

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

如果我连接线到这一点一个DataGridView我看到10行3列,列标题显示的字段的名称。

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.

我不知道它是多么容易扭转的行和列所以,随着我结束了3行10列,显示在行标题字段名相同的数据。

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.


我可以做一些事情manualy,像一个覆盖OnPaint方法,直接绘制字段名称到行标题单元格,但我正在寻找的东西更自动化。

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.

再次有manualy交换价值的建议,但除非我让所有值的字符串,这是行不通的。 3列数据表 - 美孚,酒吧,和巴兹类型整型,浮点和字符串是不会转

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.

即使我管理的所有这些手动更改,我的数据网格的复选框列,组合框列 - 没有这样的行对应的存在 - 没有复选框行或组合框的行。在我目前只需要告诉编译器添加ComboBoxColumn我不得不重新编写,以便被单独生成的每一个细胞。

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.

理想我像暴露了DataGridView的所有功能,另外还有布尔财产换位一TransposableDataGridView。这样,我可以离开我的所有代码,正是因为它是 - 我不会有改变,除了网格的任何类型

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! :)

推荐答案

您可以创建新的DataTable,添加collumns合适的号码,然后复制值从一个表给对方,只是交换行和colums。

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天全站免登陆