Solved

Display records in my 2nd Datagrid (vb.net)

Posted on 2004-10-13
8
259 Views
Last Modified: 2010-04-23
Hi, I trying to explain what I have done and my problems...

In Design Form, I have 2 DataGrid (DataGrid1 & DataGrid2), one TextBox1, one btnLoad buton & one btnOK.

I have import Data from Excel (.xls) into DataGrid1, please take a look my code;

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim NewRecord As Boolean = False

        MyConnection = New System.Data.OleDb.OleDbConnection( _
                      "provider=Microsoft.Jet.OLEDB.4.0; " & _
                      "data source=c:\data.xls; " & _
                      "Extended Properties=""Excel 8.0;IMEX=1;ReadOnly:=False;UpdateLinks:=0""")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
              "select [Batch Number], [Chassis Number] from [sheet1$]", MyConnection)

        Dim dsi As DataSet
            dsi = New System.Data.DataSet

            MyCommand.Fill(dsi)
            MyConnection.Close()

        DataGrid1.DataSource = dsi.Tables(0)

    End Sub


DataGrid1 will display the data as below;

Batch Number Chassis Number
6249 0123456ABCDEF123456
6249 ABC1234AJF12567PIHN
6250 0876HGIYFNB12347809
6250 012365POMNB1245987


Below is my DataGrid2 code;

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds As New DataSet
        Dim dt, dt2 As DataTable

        dt2 = ds.Tables.Add()
        dt2.Columns.Add("Cin Number")
        dt2.Columns.Add("Batch Number")
        dt2.Columns.Add("Chassis Number")
        DataGrid2.DataSource = dt2
End Sub

So in DataGrid2, it look like this (no records inside, just have 3 columns here);

Cin Number Batch Number Chassis Number

When I finished Load the data from Excel into the DataGrid1 & in DataGrid2 we can see the 3 columns there, Cin, Batch & Chassis Number without record.

After that I will use the Barcode Scanner to scan the Cin Number(from barcode label) into the TextBox1 (Cin # will display in the Textbox1), if the the 9th - 12th of the Cin Number are MATCH the Batch Number, one row of records will display in DataGrid2.

Example if this Cin number is XC9050426249L1JL, the 9th - 12th number is 6249 so it is MATCH to 6249 Batch Number, in the DataGrid2 the result will be like this

Result in DataGrid2 when finished scan;

Cin Number Batch Number Chassis Number
XC9050426249L1JL 6249 0123456ABCDEF123456
 
My Questions is..
How do I write a code retrieved the records, when I finished scan the Cin# in Textbox1 the record will automatically/immediately display in the DataGrid2?

In DataGrid2 should have a below record..

Cin Number Batch Number Chassis Number
XC9050426249L1JL 6249 0123456ABCDEF123456

If can please sent me codes...

Hope can understand what I am saying...
0
Comment
Question by:kaifong78
  • 4
  • 2
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Hi Kaifong78,

a question

6249 0123456ABCDEF123456
6249 ABC1234AJF12567PIHN
6250 0876HGIYFNB12347809
6250 012365POMNB1245987

the batchnumber doesn't seem to be unique how will you choose between them

if this is just a typo, then the next question would be is batchnumber the primary key?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
I'm assuming batchnumber is the primary key.


do something like this in the textchanged event of textbox1 (cin)

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim dr As DataRow = ds.Tables(0).Rows.Find(TextBox1.Text.Substring(8, 4))
        If Not IsNothing(dr) Then
            Dim dr2 As DataRow = ds.Tables(1).NewRow
            dr2.Item("Cin Number") = TextBox1.Text
            dr2.Item("Batch Number") = dr.Item("batchnumber")
            dr2.Item("Chassis Number") = dr.Item("chassisnumber")
            ds.Tables(1).Rows.Add(dr2)
        else
             msgbox("batchnumber not found")
        End If

    End Sub
0
 

Author Comment

by:kaifong78
Comment Utility
Answer your question...

One batch number can have many Chassis number...

eg.
6249   0123456ABCDEF123456
6249   ABC1234AJF12567PIHN
6250   6250 0876HGIYFNB12347809

this 6249 batch number have 2 Chassis number and 6250 have only one chassis number...

if I scan this Cin number (in testbox) XC9050426249L1JL , 9th - 12th number are match to batch number (6249), the in the DataGrid2 it will display

XC9050426249L1JL   6249   0123456ABCDEF123456
                               6249   ABC1234AJF12567PIHN


the user will choose this XC9050426249L1JL  6249  0123456ABCDEF123456 from the DataGrid2 and print it out, next time he will choose XC9050426249L1J 6249  ABC1234AJF12567PIHN to print out  from DataGrid2 again.

So I don't need the primary key rite? user just import the Batch and Chassis number from Excel, if the Cin# number match with the Batch number, records will display in the DataGrid2.

Do you get what I mean? :)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Yes I think I do, then this should work


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim dv As DataView = ds.Tables(0).DefaultView
        dv.Sort = "batchnumber"
        Dim drs() As DataRowView
        drs = dv.FindRows(TextBox1.Text.Substring(8, 4))
        For Each dr As DataRowView In drs
            Dim dr2 As DataRow = ds.Tables(1).NewRow
            dr2.Item("Cin Number") = TextBox1.Text
            dr2.Item("Batch Number") = dr.Item("batchnumber")
            dr2.Item("Chassis Number") = dr.Item("chassisnumber")
            ds.Tables(1).Rows.Add(dr2)
        Next

    End Sub
0
 

Author Comment

by:kaifong78
Comment Utility
Hi,

I have tried your code...but I found 2 errors here....

Frist is when I scan this Cin # in Textbox1 XC9050426249L1JL, it display X in TextBox, always display the 1st character...

After that this error message again...
 
Dim dv As DataView = ds.Tables(0).DefaultView  ---> An unhandled exception of type "System.IndexOutOfRange Exception; occurred in System.data.dll.
Additional Information: Cannot find table 0

0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 125 total points
Comment Utility
Well, the second error is because, I think in your case it would be probably dsi instead of ds and dsi should be declared public in the form not local in your btnLoad_Click event.

The first error is probably because your scanning puts the code in one by one, meaning it will fire the textchange event for every character. What you could do about that is probably testing for the correct lenght of your scanned string, something like
(I hope the cin code is always the same lenght)

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
 If TextBox1.Text.Length = 16 Then
        Dim dv As DataView = ds.Tables(0).DefaultView
        dv.Sort = "batchnumber"
        Dim drs() As DataRowView
        drs = dv.FindRows(TextBox1.Text.Substring(8, 4))
        For Each dr As DataRowView In drs
            Dim dr2 As DataRow = ds.Tables(1).NewRow
            dr2.Item("Cin Number") = TextBox1.Text
            dr2.Item("Batch Number") = dr.Item("batchnumber")
            dr2.Item("Chassis Number") = dr.Item("chassisnumber")
            ds.Tables(1).Rows.Add(dr2)
        Next
end if
    End Sub
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now