简体   繁体   English

在按钮的click事件中访问2个单独的gridview的数据

[英]Accessing 2 separate gridview's data in a button's click event

I'm new to C# (mostly do SQL development) and I'm having trouble getting my head around .net events and how to bring it all together. 我是C#的新手(大多数人从事SQL开发),但是我对.net事件以及如何将它们整合在一起遇到困难。 I have a form that looks roughly like this: 我有一个大致如下所示的表格:

gridView1
gridView2     btn_Addpoint
gridView3     btn_RemovePoints

So on my form I have 3 Gridcontrols, the top one has the parent rows and the second one has the children. 所以在我的表单上,我有3个Gridcontrols,最上面的一个有父行,第二个有子行。 Then I have 2 buttons that can either add selected points from the 2nd grid to the 3rd grid and the remove button to remove them from the 3rd grid. 然后,我有2个按钮,可以将选择的点从第二个网格添加到第三个网格,而删除按钮则可以将它们从第三个网格中删除。

So when I add the button click event I have this: 因此,当我添加按钮单击事件时,我会得到以下信息:

btnAddPoint_Click(object sender, EventArgs e)

I understand the sender is the actual button and I have googled a bit and discovered the Tag property to create a pointer to one of the grids which you can Cast inside this button event to access the actual grid but it seems like it can only have 1 Tag so what about my other 2 grids? 我知道发件人是实际的按钮,我在谷歌上搜索了一下,发现Tag属性创建了一个指向其中一个网格的指针,您可以在此按钮事件中进行强制转换以访问实际的网格,但似乎只能包含1个标记,那么我的其他2个网格呢?

The whole purpose is to check whether duplicates are added and I also need to change the colour of some of the rows based on conditions. 整个目的是检查是否添加了重复项,我还需要根据条件更改某些行的颜色。

How do I "see" all 3 grids inside the 2 button events to get access to their rows? 如何“查看” 2个按钮事件中的所有3个网格以访问其行? Should I make them global variables and somehow update them using events or setup a relationship somehow? 我应该使它们成为全局变量,并以某种方式使用事件更新它们还是以某种方式建立关系? I just need to be steered in the right direction here of what to do / what the best practise is, but some code would also be appreciated. 我只需要在正确的方向上指导做什么/最佳实践是什么,但是一些代码也将受到赞赏。

If the controls are all on the same form with the button event handler you can access the grids via the name you give them in the designer (or if you haven't named them they will be datagridview1, datagridview2, datagridview3). 如果控件都与按钮事件处理程序位于同一表单上,则可以通过在设计器中为其指定的名称来访问网格(或者,如果未命名它们,它们将是datagridview1,datagridview2,datagridview3)。

(Would have posted as a comment but not got the "rep" yet) (将以评论形式发布,但尚未获得“代表”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM