如何在字符串变量的datagrid列中存储多个单元格值 [英] how to store multiple cell values in a datagrid column in a string variable

查看:55
本文介绍了如何在字符串变量的datagrid列中存储多个单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的名字是Ade,我在VB.net中有一个问题,在项目中有以下,form1,form2和模块1,下面是数据网格工作的公平表示,位于form1

Hi my name is Ade and I have a question in VB.net, in the project are the following, form1, form2, and module 1, below is fair representation of a datagrid am working which is located in form1

---------------------------------------------
id   |quantity  |   item       | cost  | idtype |
-------------------------------------------------
1    |       1  | shirt        |  200  |    G   |
--------------------------------------------------
2    |       0  |  blue        |    0  |    C.  |
--------------------------------------------------
3    |       0  | cotton.      |    0  |    M   |
--------------------------------------------------
4.   |       0  |press only.   | -100  |    E   |
--------------------------------------------------
5    |       0  | children     |  -50  |     E  |
--------------------------------------------------





当我点击此表单上的某个按钮时,会出现另一个表单,其中包含第2列(项目列)中的单元格值另一种形式的datagridview,form2。 Form2有标签控件,每个值都分配给,下面是代码:



模块



when I click a button on this form another form appears bearing the cell values in column 2(item column) in the datagridview on another form, form2. Form2 has label controls on which each of these values are assigned to, below is the code:

Module

Module1

Public taggarmentname as string
Public taggarmentcost as integer
Public taggarmentcolor as string
Public taggarmentfabric as string

End Module



在form1


On form1

Private sub btndisplay_click

For r as integer = 0 To  hdgvlaundry.rows.count -1

If hdgvlaundry.rows(r).cells(4).value ="G" then
Taggarmentname= hdgvlaundry.rows(r).cells(2).value 
Taggarmentcost=hdgvlaundry.rows(r).cells(3).value 

Elseif hdgvlaundry.rows(r).cells(4).value ="C" then
Taggarmentcolor = hdgvlaundry.rows(r).cells(2).value 

Elseif hdgvlaundry.rows(r).cells(4).value ="M" then
Taggarmentfabric = hdgvlaundry.rows(r).cells(2).value 


Form2.show dialog
Endif
Next
End sub





在表格2





On form2

Private sub form2_load
Lblgarmentnametag.text= taggarmentname
Lblgarmentpricetag.text= taggarmentcost
Lblgarmenttagcolor.text= taggarmentcolor
Lblgarmenttagfabric.text= taggarment

End sub





所有这一切都运行良好,问题是我不知道如何在第2列中存储具有其idtype的值列(4)等于值E,它出现两次。



我的意思是我希望存储值'press only'和'children'in一个字符串变量,并将其显示在form2上的标签中,如下所示:



'仅按/儿童'。



其他行的idtype列(4)总是不同,即出现一次,但有时idtype列(4)等于'E'的行可能会出现多次,因此需要存储多个具有值'E'的idtype列(4)的列(2)的单元值在变量中。请有人帮忙。



在我的form1代码中,要将单元格值存储在第2列(项目列),我使用条件语句:







All this works well, the issue is that I do not know how to store the value in column 2 which has its idtype column(4) equal to value "E" which appears twice.

what I mean is that I wish to store the values 'press only' and 'children' in one string variable and have it displayed in a label on form2 as follows:

'press only/children'.

The other rows always have their idtype column(4) distinct, that is appearing once, but sometimes the row with a idtype column(4) equal to 'E' may appear more than once, hence the need to store multiple cell values of column(2) with idtype column(4) of value 'E' in a variable. Please can someone help.

In my code on form1, to store the cell value on column 2 (item column) I use the condition statement:


For r as integer = 0 To hdgvlaundry.rows.count -1

If hdgvlaundry.rows(r).cells(4).value ="G" then
Taggarmentname= hdgvlaundry.rows(r).cells(2).value
Taggarmentcost=hdgvlaundry.rows(r).cells(3).value





现在这是简单直接,因为idtype列(第4列)的值='G'只出现一次。



所以也是idt ype列(第4列),其值='C',也只出现一次:



Now this is easy and straight forward because the idtype column (column 4) with value = 'G' appears only once.

So also is idtype column (column 4) with value = 'C' which also appears only once :

Elseif hdgvlaundry.rows(r).cells(4).value ="C" then
Taggarmentcolor = hdgvlaundry.rows(r).cells(2).value



idtype列(第4列)的情况类似,值='M'也只出现一次




Similarly is the case for idtype column (column 4) with value = 'M' which also appears only once

Elseif hdgvlaundry.rows(r).cells(4).value ="M" then
Taggarmentfabric = hdgvlaundry.rows(r).cells(2).value





但对于值为'E'的idtype列(第4列),它出现不止一次(2次)和th e对应的项目列(第2列)有两个值('仅按'和'子'),我需要将它们存储在一个变量中。那么如何将两个不同行的单元格值存储在一个变量中



亲切的问候



Ade



[评论]使用正确的格式![/ Comment]

推荐答案

我建议回到基础......你必须处理数据,而不是字符串表示!



你需要的只是创建自定义< a href =https://msdn.microsoft.com/en-us/library/ms973814.aspx>类 [ ^ ],界面 [ ^ ]和自定义类集合 [ ^ ]。



自定义类的定义可能如下所示:

界面:

I'd suggest to get back to basics... You have to work on data, not on its string representation!

All what you need is to create custom class[^], interface[^] and custom class collection[^].

The definition of custom class may look like:
Interface:
Public Interface IMyData
    Property Id As Integer
    Property quantity As Integer
    'and so on...
End Interface





等级:



Class:

Public Class MyData 
        Implements IMyData

    Private id As Integer = 0
    Private quantity As Integer = 0
    Private item  As String = String.Empty
    Private cost  As Integer = 0
    Private idtype As String = String.Empty

    Public Property Id As Integer Implements IMyData
        Get 
            Return id
        End Get
        Set (value As Integer)
            id = value
        End Set 
    End Property

    'and so on...

End Class





收藏类:



Collection Class:

Public Class MyDataCollection 
     Implements Collections.CollectionBase
     'define Add and Remove methods
     'define Item property which returns MyData class

End Class





模块:



Module:

Public Module Module1
'define global variable
Public oMdc As MyDataCollection  = Nothing

End Module





现在,您可以在表单之间共享数据。



Now you can share data between forms.

oMdc = New MyDataCollection
Dim oMd As IMyData = New MyData

With oMd
    .Id = 1
    'other properties
End With
'add custom class to the collection
oMdc.Add(oMd)


'later
DataGridView.DataSource = oMdc





这只是一个想法,而不是完整的解决方案。



It's just an idea, not complete solution.


这篇关于如何在字符串变量的datagrid列中存储多个单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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