DataGrid View-Urgent

Hi
I have windows where i enter data in one windows form, based on these, i need to display the retreived data from the oracle database in a data grid view of another windows form. how do i display it
LVL 1
SGUDAPATAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Priest04Connect With a Mentor Commented:
SGUDAPAT, you didnt understand what we are doing here. So I will try to explain again.

>>And also near the constructore new keyword should call initialize component method.

You are correct about this, add this line to constructor. Now to the explanation

Requirement was to pass information from one form to another (ex from Form1 to Form2). One of the ways to pass information from one form to another is to pass information to Form2 constructor.

When Form is created, a contructor is being called, and as you have pointed above, in constructor InitializeComponent() procedure is being called (this proeedure is located in designer code), which leads to all controls that are placed on the form (like buttons, textboxes, etc) being initialized. Default constructor does not accept any parameters (note: in VB, default constructor is not shown in code, but when code is compiled, it is automatically added, because it must exist)

Public Sub New()
    InitializeComponent()
End Sub

we need to change this contructor to access parameters in order to pass information. Since we need to pass 2 paramteters, we will add two parameters


Public Sub New(startTag As Integer, endTag As Integer)
    InitializeComponent()
End Sub

and this will allow us to pass information. How we do it. To create an instance of the form with DEFAULT contructor, we use this code

Dim frm As New Form1() ' note that between parentheses there is no parameter, since we call the defailt construcot.

At this point, a form and all its controls are being instantiated, but still not visible. To make the form visible, we call either Show or ShowDialog methods.

Now, lets see how we should instantiate form with contructor that accepts parameters, like in above example

Public Sub New(startTag As Integer, endTag As Integer)
    InitializeComponent()
End Sub

As you can see contructor takes 2 paramteres of type integer. So, when creating form object, we need to pass 2 parameters

Dim frm As New Form1(5, 10) ' we pass two parameters with values of 5 and 10)
frm.ShowDialog()

so, in your case, it should be

If isAdmin = 1 Then
    Dim frm As New FrmCnt(Convert.ToInt32(STARTtxt,Text), Convert.ToInt32(EndTxt.Text))
            With FrmCnt
                .ShowDialog()
            End With
0
 
Priest04Commented:
I dont understand the quesion. You want to display data from oracle database on datagridview? Pull the data into datatable and bind datagridview to datatable.

Goran
0
 
Shaju KumbalathDeputy General Manager - ITCommented:
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
SGUDAPATAuthor Commented:
Goran: I want to display the data based the selection which I entered in the first on the second windows.
Like I enter ID From:1000
                  ID To     :2000
In first windows form, i want to display the data related to 1000 to 2000 ID in the data grid view of the second windows form.
0
 
Priest04Commented:
SGUDAPAT, what exactly do you want help with?

1) Do you know how to create OracleConenction?
2) How does the SELECT query looks like?
3) Shold data be read only?
    3a) if yes, do you know how to create OracleCommand and OracleDataReader objects?
    3b) if no, do you know how to create OracleOleDbDataAdapter?
0
 
Priest04Commented:
sorry, it should have been
3b) if no, do you know how to create OracleDataAdapter?
0
 
SGUDAPATAuthor Commented:
yes i know know to create the Oracle Data Adapter and I can also get into the data table by fill it with data set, but i am sure how i can get the data selection from one form and display it in another form(data grid)
0
 
Priest04Commented:
ok so post the code here where you create conenction and data adapter, and also tell me what field is used to filter data?

example

SELECT * FROM TableName WHERE field_name BETWEEN 1000 AND 2000
0
 
SGUDAPATAuthor Commented:
i have the code like this
Here is where I have my connections
--------------------------
Public Function GetDataSet(ByRef ds As DataSet, _
                               ByVal sSQL As String, _
                               Optional ByVal tableName As String = "Table") As Boolean
        Try
            DA.SelectCommand = New OleDbCommand(sSQL)
            DA.SelectCommand.Connection = dbConn
            DA.Fill(ds, tableName)
            GetDataSet = True
        Catch ex As Exception
            writeErrorLog(ex)
            GetDataSet = False
        Finally
            DA.SelectCommand.Dispose()
        End Try
    End Function
---------------------------------------------------------
Here is the select query
---------------------------------------------------------
Private Sub GetSQL()
        Dim DS As New DataSet
        sAREA = SelectAreaCBx.SelectedItem.ToString.Trim
 sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _
 "storage_area, level1, level2, level3, level4, plant " & _
 " from thi_inv_tags_temp where area = '" & sAREA & "'" & _
 "and tagnum between "&STARTTxt.Text&" and "&EndTxt.Text & " order by tagnum "
        If GetDataSet(DS, sql) Then
            With DS.Tables("Table")
 
                If .Rows.Count > 0 Then
                    For Each row As DataRow In .Rows
                        With row
                        dgvCount.DataSource = DS.Tables("Table")              
                        End With
                    Next
                End If
            End With
        End If
    End Sub
---------------------------------
Now i do I get data displayed on the second form
I enter data of ranges start tag and end tag in the first form, I select area also in the first form, but I want the data from table to be displayed in the second form.

Open in new window

0
 
Priest04Commented:
if this data should be displayed on the 2nd form (form2), then you should place this two functions for getting dataset onto from2.


' form2 contructor
Public Sub New(startTag As Integer, endTag As Integer)
    GetSQL(startTag, endTag)
End Sub
 
Private Sub GetSQL(startTag As Integer, endTag As Integer)
        Dim DS As New DataSet
        sAREA = SelectAreaCBx.SelectedItem.ToString.Trim
 sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _
 "storage_area, level1, level2, level3, level4, plant " & _
 " from thi_inv_tags_temp where area = '" & sAREA & "'" & _
 "and tagnum between "& startTag & " and " & endTag & " order by tagnum "
        If GetDataSet(DS, sql) Then
            With DS.Tables("Table")
 
                If .Rows.Count > 0 Then
                    For Each row As DataRow In .Rows
                        With row
                        dgvCount.DataSource = DS.Tables("Table")              
                        End With
                    Next
                End If
            End With
        End If
    End Sub

Open in new window

0
 
Priest04Commented:
of course, GetDataSet, should also be placed on Form2
0
 
SGUDAPATAuthor Commented:
will startTag and endTag values be imported directly from form1, since they are entered in form1, or should I make them public in form1
0
 
Priest04Commented:
you will pass them to form 2 over its constructor. ex

dim Form2 as New Form2(Convert.ToInt32(STARTTxt.Text), Convert.ToInt32(EndTxt.Text))
Form2.Show() ' or ShowDialog, which ever you prefer.

You could have also created SQLString in form1, and pass query to form2 (contructor would  then receive only one parameter - SQLString as String), makes no difference.
0
 
SGUDAPATAuthor Commented:
u mean to say that if I want the form2 to open when i click on Submit button in form1, then i should write in the button click:
dim Form2 as New Form2(Convert.ToInt32(STARTTxt.Text), Convert.ToInt32(EndTxt.Text))
Form2.Show()
0
 
Priest04Commented:
yes
0
 
SGUDAPATAuthor Commented:
I really for your appreciate for your patience to explain me the possible solution.

This whole application contains of 8 windows forms.
I have Main where I hae have toolStrip on which I have lables,
The user can select the label he wants and that form opens.
When I am adding the above code in the form1 and form2
I am getting an unhandled exception like

"InhandledOperationEception was unhandled

An error occured creation the form.See Exception.InnerException for the details.the error is:Oject reference not set to an instance of an object."

I am gtting this exception on beow line in the toolStripMenuItemClick event.
FrmCnt .ShowDialog()
0
 
Priest04Commented:
what is the rest of the code for this event?
0
 
SGUDAPATAuthor Commented:
Here is the code
  Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
        If isAdmin = 1 Then
            With FrmCnt
                .ShowDialog()
            End With
        ElseIf isAdmin = 2 Then
            With FrmCnt
                .ShowDialog()
            End With
        Else
 
            With FrmLogInScreen
                .ShowDialog()
            End With
        End If
    End Sub

Open in new window

0
 
SGUDAPATAuthor Commented:
I worked before added the code I am not sure what went wrong, here is the code once with i added.

And also near the constructore new keyword should call initialize component method.
In the button click of form1
-------------------------------- 
Dim FrmEnterCountData As New FrmEnterCountData(Stag, ETag, sAREA)
 FrmEnterCountData.ShowDialog()
-------------------------
form2
-------------------------
 Public Sub New(ByVal startTag As Integer, ByVal endTag As Integer, ByVal sAREA As String)
        GetSQL(startTag, endTag, sAREA)
    End Sub
 
    Private Sub GetSQL(ByVal startTag As Integer, ByVal endTag As Integer, ByVal sAREA As String)
        Dim DS As New DataSet
        sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _
        "storage_area, level1, level2, level3, level4, plant " & _
        " from tags_temp where area = '" & sAREA & "'" & _
        "and tagnum between " & startTag & " and " & endTag & " order by tagnum "
-------------
        If GetDataSet(DS, sql) Then
            With DS.Tables("Table")
 
                If .Rows.Count > 0 Then
                    For Each row As DataRow In .Rows
                        With row
                            dgvCount.DataSource = DS.Tables("Table")
                        End With
                    Next
                End If
            End With
        End If
    End Sub

Open in new window

0
 
SGUDAPATAuthor Commented:
Thanks a bunch, i got it working.
I am have final question for you, i think its a simple one, but i don't know how to do it.
i have the windows form, on which i display my data grid view, i set that windowSatae property to Maximized, so that it open like a maximized window that window open, but my data grid view is not getting maximized with the window, how do I autosize the data grid view.
0
 
SGUDAPATAuthor Commented:
got it working, thanks once again
0
 
Priest04Commented:
Set Anchor property to Top, Left, Right and Bottom.
0
 
SGUDAPATAuthor Commented:
If my data grid grid view returns zero rows i want it prompt me that i need to enter range of tags that exist.
I tried it using this code

It is giving me a OjectDisposedException wasn unhandled on the
FrmEnter.showDialog
in the form1
What should I do?
form2---------------------
----------------
  Private Sub GetSQL(ByVal startTag As Integer, ByVal endTag As Integer, ByVal sAREA As String)
        Dim DS As New DataSet
        sql = "select TAGNUM,QTY,UOM,INV_ITEM_ID,ITEM_DESCR," & _
        "storage_area, level1, level2, level3, level4, plant " & _
        " from tags_temp where area = '" & sAREA & "'" & _
        "and tagnum between " & startTag & " and " & endTag & " order by tagnum "
        If GetDataSet(DS, sql) Then
            With DS.Tables("Table")
 
                If .Rows.Count > 0 Then
                    dgvCount.DataSource = DS.Tables("Table")
                    rowLbl.Text = "Rows : " & .Rows.Count
                Else
                     Me.Close()
                    Me.Dispose()
                End If
            End With
        End If
    End Sub

Open in new window

0
 
Priest04Commented:
SGUDAPAT, we could go on like this forever. You should create new threads for new questions.
0
 
SGUDAPATAuthor Commented:
:) seems like.
thanks Goran,figured it out, don't need a new thread right now :))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.