Solved

can an arraylist have 2 columns?

Posted on 2011-03-22
9
606 Views
Last Modified: 2012-05-11
I am scanning inventory into a datagrid and in the process getting some information on each item.  I'd like to have 2 separate columns but can't seem to get this to work with an array list.  I am kind of new to array's.  is this possible or should I do this an easier way.  i am displaying the data in Gridviewer on Visual studio 2005.  thanks.


    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim ItemList As New ArrayList()
        Dim strSql1 As String = ""
        Dim strOrigValue As String = TextBox1.Text

        If Not Session.Item("sessItemList") Is Nothing Then
            ItemList = Session("sessItemList")
        End If

        If TextBox1.Text <> "" Then

            strSql1 = "select top 1 ITEMNMBR from IV00200 where SERLNMBR like '%02608520462%'"    ' for example use only '
            TextBox1.Text = TextBox1.Text & " || " & exExecuteScalar(strSql1)

            If ItemList.Contains(TextBox1.Text) Then
                lblError2.Text = "Error! Duplicate Values are not allowed."
            Else
                ItemList.Add(TextBox1.Text)
                lblError2.Text = ""
            End If

        End If

        Session("sessItemList") = ItemList

        GridView1.DataSource = Session("sessItemList")
        GridView1.DataBind()

        TextBox1.Text = ""
        TextBox1.Focus()

    End Sub
0
Comment
Question by:purdyra1
9 Comments
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35188577
using datatable would be an easy approach for that.
eg,

// Here we create a DataTable with four columns.

	DataTable table = new DataTable();
	table.Columns.Add("Dosage", typeof(int));
	table.Columns.Add("Drug", typeof(string));
	table.Columns.Add("Patient", typeof(string));
	table.Columns.Add("Date", typeof(DateTime));

	//
	// Here we add five DataRows.
	//
	table.Rows.Add(25, "Indocin", "David", DateTime.Now);
	table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
	table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
	table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
	table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
	

Open in new window


0
 
LVL 10

Expert Comment

by:khan_webguru
ID: 35188662
Hello Bro!

I did that in past by the help of this code:

 
Public Class withPICS
    Dim _pic As system.byte
    Dim _id As integer
 
    Sub New(ByVal pic As system.byte, ByVal id As integer)
        _pic = pic
        _id = id
    End Sub
 
    Public ReadOnly Property pic() As system.byte
        Get
            Return _pic
        End Get
    End Property
 
    Public ReadOnly Property id() As integer
        Get
            Return _id
        End Get
    End Property
End Class
 
xarray.ADD(new withPICS(myreader.getvalue(0), myreader.getvalue(1))
mydatagridview.datasource = xarray '''displays

Open in new window


I Think

xarray.item(i) .pic() ' would get the picture
xarray.item(i).id()  ' would get the id

PS. Using the class withPICS will take up more memory then two arrays, and will run slower.


Hope this example will help u to fix ur problem.

Regards,

Asif Ahmed Khan
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35188721
Hi,
You can use some of your Custom class and then use ArrayList of that objects. as following

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim ItemList As New ArrayList()
        Dim strSql1 As String = ""
        Dim strOrigValue As String = TextBox1.Text

        If Not Session.Item("sessItemList") Is Nothing Then
            ItemList = Session("sessItemList")
        End If

        If TextBox1.Text <> "" Then

            strSql1 = "select top 1 ITEMNMBR from IV00200 where SERLNMBR like '%02608520462%'"    ' for example use only '
            TextBox1.Text = TextBox1.Text & " || " & exExecuteScalar(strSql1)

            Dim data As MyDataClass = New MyDataClass(TextBox1.Text, "someothe data from qry")

            If ItemList.Contains(data) Then
                lblError2.Text = "Error! Duplicate Values are not allowed."
            Else
                ItemList.Add(data)
                lblError2.Text = ""
            End If

        End If

        Session("sessItemList") = ItemList

        GridView1.DataSource = Session("sessItemList")
        GridView1.DataBind()

        TextBox1.Text = ""
        TextBox1.Focus()

    End Sub

and here is your custom class
Public Class MyDataClass

    Public Sub New()

    End Sub

    Public Sub New(ByVal v1 As String, ByVal v2 As String)
        Val1 = v1
        Val2 = v2
    End Sub
    Public Val1 As String
    Public Val2 As String

End Class

Make sure you define and assign column to correct datafield in the gridview
Thanks
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:purdyra1
ID: 35192069
Thank you to all...

IJZ:
I tried your approach but I am getting an error.

ERROR:  
error:
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: A field or property with the name '03712758111 || BS_NOK_BX' was not found on the selected data source.

here is the gridview info I have that I inserted into the code you gave me.:


        Dim bf As New BoundField
        bf.DataField = "Val1"
        GridView1.Columns.Add(bf)

        GridView1.DataSource = Session("sessItemList")(0)
        GridView1.DataBind()
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35192302
GridView1.DataSource = Session("sessItemList")
instead of GridView1.DataSource = Session("sessItemList")(0)
0
 

Author Comment

by:purdyra1
ID: 35192358
I changed it (I did try it before as well) and now I am getting:

Additional information: A field or property with the name 'Val1' was not found on the selected data source.

also, here is what I am seeing in the command window

>? Session("sessItemList")(0).Val1

"03712758738 || BS_NOK2125HS_BX" {String}
    String: "03712758738 || BS_NOK2125HS_BX"
0
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 500 total points
ID: 35192415
ok,
please change class with following

Public Class MyDataClass

    Public Sub New()

    End Sub

    Public Sub New(ByVal v1 As String, ByVal v2 As String)
        Val1 = v1
        Val2 = v2
    End Sub
    Public Property Val1() As String
        Get
            Return _Val1
        End Get
        Set(ByVal value As String)
            _Val1 = value
        End Set
    End Property

    Public Property Val2() As String
        Get
            Return _Val2
        End Get
        Set(ByVal value As String)
            _Val2 = value
        End Set
    End Property



    Public _Val1 As String
    Public _Val2 As String

End Class
0
 

Author Closing Comment

by:purdyra1
ID: 35198638
Works great now, thanks for your help.
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35198657
you are most welcome
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question