Click here to Skip to main content
15,851,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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  |
--------------------------------------------------


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
VB
Module1

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

End Module

On form1
VB
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


On form2

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

End sub


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:


VB
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


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 :
VB
Elseif hdgvlaundry.rows(r).cells(4).value ="C" then
Taggarmentcolor = hdgvlaundry.rows(r).cells(2).value

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

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


But for idtype column (column 4) with value = 'E' it appears more than once (2 times) and the corresponding item column (Column 2) has 2 values('press only' and 'children'), which I need to store, both in a variable. So how do I store both cellvalues of 2 different rows in a variable

Kind regards

Ade

[Comment]Use proper formatting![/Comment]
Posted
Updated 8-Sep-15 3:01am
v4
Comments
Maciej Los 7-Sep-15 15:59pm    
Sorry, but your question is unclear. Please, be more specific and provide more details...
mabadeje 7-Sep-15 16:35pm    
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

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

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


But for idtype column (column 4) with value = 'E' it appears more than once (2 times) and the corresponding item column (Column 2) has 2 values('press only' and 'children'), which I need to store, both in a variable. So how do I store both cellvalues of 2 different rows in a variable
Maciej Los 7-Sep-15 16:43pm    
Ok, move this explanation to the question. Use "Improve question" widget.
mabadeje 7-Sep-15 17:09pm    
How can I move this explanation to the question?
Maciej Los 7-Sep-15 17:16pm    
You can do that any time. Move the mouse cursor to the bottom-right corner of your question and you'll see "Improve question" widget ;)

1 solution

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:
VB
Public Interface IMyData
    Property Id As Integer
    Property quantity As Integer
    'and so on...
End Interface


Class:
VB
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:
VB
Public Class MyDataCollection 
     Implements Collections.CollectionBase
     'define Add and Remove methods
     'define Item property which returns MyData class

End Class


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

End Module


Now you can share data between forms.
VB
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900