将数据从1 DGV发送到另一个DGV中的特定列 [英] Sending data from 1 DGV to the specific columns in the other

查看:79
本文介绍了将数据从1 DGV发送到另一个DGV中的特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我不小心删除了上一个问题,但我的问题已经解决了......

我在您的网站上有一些很好的帮助,找到了我正在努力解决的一些问题,但现在我自己也得到了一个问题:

我使用了这个页面中的代码:link [^]

,它很棒。它在第二个数据网格中添加了新的coloms并填充它们。但是,是否可以将选定的数据添加到excisting列中的新行中?如果已经填充了第1行和第2行(例如),那么它会自动创建一个新行(或者这是不可能的?)

这样我可以用2中的数据填充数据网格视图其他datagridviews将节省我一段时间。



更新

每个请求,我已经尝试过的代码。如果你想填充一个空的dgv,它可以工作,但我想填充一个已经填充的dgv。

Hello, i accidentally deleted the previous question but my problem istn solved yet so....
I had some great help on your site, found the solution to some stuff i was struggling with, but now i got a question myself:
I used the code from this page: link [^]
and it works great. it adds new coloms the the 2nd datagrid and fills them. However, Is it possible to have it add the selected data into new rows in excisting columns? and then so that it wil automatically create a new row if row 1 and 2(for example) are already populated(or is this not possible? )
That way i can fill a datagridview with data from 2 other datagridviews which will save me quite some time.

Update
Per request, the code i already tried. It works if you want to fill a empty dgv, but i want to fill a allready populated dgv.

Private Sub btncopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
        Dim c, t As Integer
        'dg2.Columns.Clear()

        For t = 0 To DataGridView1.Columns.Count - 1
            DGV2.Columns.Add(DataGridView1.Columns(t).Clone())
        Next
 
        For c = 0 To DataGridView1.Rows.Count - 1
 
            If DataGridView1.Rows(c).Cells(4).Value = True Then
                DGV2.Rows.Add(DataGridView1.Rows(c).Cells(0).Value, DataGridView1.Rows(c).Cells(1).Value)
                DGV2.Columns(2).Visible = False
                DGV2.Columns(3).Visible = False
 
            End If
 
        Next
    End Sub



经过一些评论后,我得到了这段代码。


And after some comments i got this code.

For i As Integer = 0 To Rantsoengrid.Rows.Count - 1
        If Rantsoengrid.Rows(i).Cells(3).Value = True Then
            DGV2.Rows.Add(Rantsoengrid.Rows(i).Cells(3).Value, _
            Rantsoengrid.Rows(i).Cells(2).Value, _
            Rantsoengrid.Rows(i).Cells(4).Value, _
            Rantsoengrid.Rows(i).Cells(6).Value)

        End If
    Next

End Sub



更新

好​​的,愚蠢的我。在代码中引用了错误的datagridview。

现在剩下的就是将数据从字符串转换为布尔值。 (我知道它的真或假)我会自己想出那个,但是当然,如​​果你知道答案,可以随意张贴以供将来参考(对于其他人)


update
Okay, stupid me. referenced to the wrong datagridview in the code.
Now all that remains is converting the data from a string to boolean. (of which i know its true or false)i wil ltry to figure that one out myself but ofcourse, if you know the answer, feel free to post it for future reference (for other people)

De conversie van tekenreeks  naar type Boolean is ongeldig.





如果我使用原始datagridvieuw中的单元格( 0,1,2,3)而不是我想要将数据复制到(2,3,4,6),然后我得到程序无法添加行的错误,因为datagridview是数据绑定。(删除绑定是不是一个选项,由于datagridview中的其他数据)



好​​的,更新因为我现在解决了一些问题,但不是这个。

我认为我们需要处理的方式是错误



And, if i use the cells from the original datagridvieuw(0,1,2,3) instead of the ones i want to copy the data to (2,3,4,6) then i get the error that the programm cant add rows because the datagridview is databound.(removing the binding is not a option, due to the other data in the datagridview)

Okay, update because i did solve some things now, but not this one.
I think the way we need to work from is the error

"Invalid Operationexception was unhandled"

这是因为错误报告声明它不能将数据复制到数据绑定datagridview。有没有办法解决这个问题?

This is because the errorreport states that it cannot copy the data to a databound datagridview. Is there any way around this?

推荐答案

我发布了你之前删除的帖子中的第二个代码块。在理解它们的工作原理以及使它们工作所需的条件之前,你不会解决使用DataGridViews遇到的各种问题。



首先你要求添加行已填充的数据网格。我给了你代码。你接受我的代码并完全复制它,但现在你得到了一个以前没有的错误。



我强烈建议两件事:(1)了解更多关于 DataGridView 和(2)逐步执行代码并检查发生了什么以及何时发生。



你有两个可能的索引:索引。 索引的代码是完全正确的,我自己写道。因为你修改了代码,我不能对你的说同样的话。



我的直觉就是说你做了数据网格中没有7列! (我可能错了,但我强烈怀疑)



我要再说一遍。行和列都基于0。这意味着你开始计数为0(而不是1)。
I posted the 2nd code block from your previous, now deleted post. You will not solve the various problems you are experiencing with DataGridViews until you understand how they work and what you need to make them work.

First you asked to add rows to an already populated datagrid. I gave you the code. You take my code and copy it exactly and yet now you getting an error you did not previously had.

I strongly suggest two things: (1) Learn more about DataGridView and (2) Step through your code and check to see what happens and when it happens.

You have two possible indexes: Rows and Columns indexes. The code for the Rows index is perfectly correct, I wrote that myself. I can not say the same for your Columns since you adapted the code.

My gut feel says you do not have 7 columns in your datagrid !!! (I might be wrong however I strongly doubt that)

I am going to say it again. Both Rows and Columns are 0 based. This means you start your count at 0 (not 1).


这篇关于将数据从1 DGV发送到另一个DGV中的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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