翻转DataGridView中的行和列 [英] Flip rows and columns in DataGridView

查看:84
本文介绍了翻转DataGridView中的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,我想添加一个按钮,当按下它时会翻转行和列。我怎么能做到这一点?



这是我到目前为止的代码:



I have a DataGridView and I want to add a button that when pressed will flip the rows and columns. How can I accomplish this?

Here is my code so far:

DataGridView ^dgvFlipedReport = gcnew DataGridView();
    int rowCount = dgvReport->Rows->Count;
    int colCount = dgvReport->Columns->Count;

    //Add a column to the new datagrid for every row in the orginal datagrid
    for(int row = 0; row < rowCount; row++)
    {
        DataGridViewTextBoxColumn ^newColumn = gcnew DataGridViewTextBoxColumn();
        dgvFlipedReport->Columns->Add(newColumn);
    }

    for(int col = 0; col < colCount; col++) //Loop through each column in the orginal datagrid
    {
        dgvFlipedReport->Rows->Add();       //This row in the new datagrid represents the column in the orginal datagrid
        for(int row = 0; row < rowCount; row++)     //Loop through each row in the orginal datagrid
        {
            dgvFlipedReport->Rows[col]->Cells[row]->Value = dgvReport->Rows[row]->Cells[col]->Value->ToString();  //I've tracked the bug to here.  This is where the error occurs but I don't know why.
        }
    }

    //Copy everything from the fliped datagrid to the original datagrid
    for(int row = 0; row < rowCount; row++)
    {
        for(int col = 0; col < colCount; col++)
        {
            dgvReport->Rows[row]->Cells[col]->Value = dgvFlipedReport->Rows[row]->Cells[col]->Value->ToString();
        }
    }







现在它产生了这个错误:



System.NullReferenceException:对象引用未设置为对象的实例。




Right now it produces this error:

System.NullReferenceException: Object reference not set to an instance of an object.

推荐答案

如果我正确理解你想要做什么,在DataGridView中没有内置支持。



对于一个简单的情况,你可以模拟这个,



为每列添加DataGridViewRow

自定义DataGridViewRow.HeaderCell.Value,您将在其中设置列标题

和隐藏列标题。

但是行仍然是行,列仍然是DataGridView中的列,所以你必须自己实现翻转轴(即:添加一行将包括添加DataGridViewColumn并用每行中给定列索引填充单元格。



我说简单情况因为如果你有更多的一种类型列中的数据(将显示在DataGridViewRow中),因为DataType(TextBox,Image,...)是在DataGridView的Column级别定义的,所以它可能是一个大问题。您可以通过在单元级别定义DataType来实现此目的,但出于性能原因不建议这样做。



希望这会有所帮助。



(最初在Stackoverflow上找到)
If I correctly understand what you are trying to do, there is no "built in" support for that in DataGridView.

For a simple situation, you can maybe simulate this by,

Adding DataGridViewRow for each column
customizing DataGridViewRow.HeaderCell.Value in which you will set your column header
and Hide Column Headers.
But rows will still be rows and columns will still be columns in DataGridView, so you have to implement the "flip axis" yourself (ie: adding a row will consist on adding a DataGridViewColumn and populate the cell with the given column index in each row)

And I said "simple situation" because if you have more one type of Data in your columns (which will be displayed in a DataGridViewRow), it can be a big deal since DataType (TextBox, Image, ...) is define at Column level in DataGridView. You can probably achieve this by define the DataType at Cell level but that is not recommended for performance reasons.

Hope this helps.

(Originally found on Stackoverflow)


一种方法可能是两个在内存中都有GridView并切换控件。

如果是GridView是在一个面板调用面板.Controls获取Control.ControlCollection

和Controls.Remove(view1)和Controls.Add(view2)



http://msdn.microsoft.com/en-us/library /system.windows.forms.control.controls.aspx [ ^ ]
One way could be two have both GridViews in memory and switch the controls.
If the GridView is in a panel call panel.Controls to get Control.ControlCollection
and Controls.Remove(view1) and Controls.Add(view2)

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx[^]


这篇关于翻转DataGridView中的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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