如何以相反的顺序在另一个gridview中的datagridview中添加列 [英] how to add column in datagridview in another gridview in reverse order

查看:58
本文介绍了如何以相反的顺序在另一个gridview中的datagridview中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我想将datagridview中的所有列添加到另一个datagridview中,但是第一个中的第一列是另一个datagridview中的最后一个列,依此类推
但我不知道这样做
谁能帮我
谢谢

hi all
i want to add all column in datagridview in another datagridview but the first column in the first one be the last in the another datagridview and so on
but i do not know to do it
can any one help me
thanks

推荐答案

如果dataGridView1是源,dataGridView2是目标

1.创建反转的列
2.此代码会将行从dataGridView1复制到反向的dataGridView2

if the dataGridView1 Is the source and dataGridView2 the destination

1. create the columns reversed
2. this code will copy the rows from dataGridView1 to dataGridView2 reversed

DataGridViewRow dr = new DataGridViewRow();
            foreach (DataGridViewRow dr_loopVariable in dataGridView1.Rows)
            {
                dr = dr_loopVariable;
                //Reverse the index of the cells not 0,1,2,3,4 make it like this 4,3,2,1,0
                dataGridView2.Rows.Add(dr.Cells[2].Value, dr.Cells[1].Value, dr.Cells[0].Value);
            }



问候,
A.Mandour



Regards,
A.Mandour


尝试一下....

Try this....

for (int i = DataGridViewSource.Columns.Count; i >= 0; i--)
{
    DataGridViewCopy.Columns.Add(DataGridViewSource.Columns[i-1].Clone() as DataGridViewColumn);
}


这应该在DataGridViewCopy中以相反的顺序复制DataGridViewSource列..
我希望这对您有帮助..要复制单元格的值,请尝试如下操作..


This should copy the DataGridViewSource colums in reverse order in DataGridViewCopy..
I hope this helps you..For copying the cells value try something like this..

DataGridViewRow row = new DataGridViewRow();  
  
        for (int i = 0; i < DataGridViewSource.Rows.Count; i++)  
        {  
            row = (DataGridViewRow)DataGridViewSource.Rows[i].Clone();  
            int intColIndex = DataGridViewCopy.Columns.Count-1; int ind=0; 
            foreach (DataGridViewCell cell in DataGridViewSource.Rows[i].Cells)  
            {  
                row.Cells[ind].Value = DataGridViewSource.Rows[i].Cells[intColIndex];  
                intColIndex--;  ind++;
            }  
            DataGridViewCopy.Rows.Add(row);  
        }  
        DataGridViewCopy.AllowUserToAddRows = false;  
        DataGridViewCopy.Refresh();  


我找到的唯一有用的链接是 ^ ].
请仔细讨论,这可能对您的实施有所帮助.

另外使用c#在Windows窗体中嵌套DataGridView(DataGridView中的DataGridView ) [ ^ ]在解决方案内部提供了一些重要的链接.

谢谢...
The only useful link I found is Nested datagrid views? [^].
Please go through the discussion, it may help in your implementation.

Also Nested DataGridView in Windows Forms using c# ( DataGridView within DataGridView)[^] provides some important links insides the solutions.

Thanks...


这篇关于如何以相反的顺序在另一个gridview中的datagridview中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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