Simple DataGridView Question - Passing Variables

I'm a semi competent VB6 programmer trying to understand .Net 2005.  This is what I am trying to do:

- I have a form that has information in a datagridview that is a very simple SELECT statement for a very simple DB.
- I have configured the form so if I click on the cell, the value is placed into a public variable configured in a module.
- I want to use that variable in the WHERE statement in another Datagridview on a different form.

From what I am understanding, using datagridview limits you to direct SQL.  In the past in VB6, I would create my ADODB connections in the code.  Open the DB using a Function, create a string using the & to add my variable after the WHERE like below:

 str = "SELECT * from Approved WHERE PatchNum = '" & varname & "'"

So, how do I do this?  In the past, Ive used LISTVIEWs in VB6 and have been easily retrieved data in DBs.  I just cannot believe that doing the simple DB manipulations in VB6 are this complicated in .Net 2005.  

What am I missing?  Does my question make sense?  

Chris, Baltmore.
clebo99Asked:
Who is Participating?
 
coopzzCommented:
Whew.. long post.

Unfortunatley the change to .net is quite significant.. once made though you'll never want to go back.. well thats what I thought anyway..

You can still to the manual population of a listview if you prefer it (sometimes you have no choice, in datagrids they have to be filled by a datasouce where a list view can be either I think.) but if you really want to understand the new data grids & ado.net is unfortunaly to read a book about it.  They'll show you how to interelate 2 querys to 2 dataviews and make them work togeather in around 10 lines of code for a parent to child records. (eg.  http://msdn2.microsoft.com/en-us/library/aa984328(VS.71).aspx )
It's really up to you what you perfer The DataGrid's are getting much better in 2005 But I think there really slow to work with especially when autosizing comes into it.
ANyway let me know how you go.
0
 
coopzzCommented:
Heres a quick example to populate in code and grab the selected value when only one is selected.
----------Code--------------
    Private Function GetData(ByVal sSQL As String) As DataTable
        Dim oComm As New SqlCommand
        Dim oAdapter As New SqlDataAdapter(oComm)
        Dim oDt As New DataTable
        With oComm
            .Connection = New SqlConnection("YourConnectinoString")
            .CommandText = sSQL
            .CommandType = CommandType.Text

            Try
                oAdapter.Fill(oDt)
            Catch ex As Exception
                .Connection.Dispose()
                'cLog.LogError(ex, sSQL , True)
            Finally
                .Connection.Close()
            End Try
            Return oDt
        End With
    End Function
    Private Sub cmdLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadData.Click
        Dim dt As DataTable = GetData("Your Select Query")
        DataGridView1.DataSource = dt
        DataGridView1.Refresh()
    End Sub
    Private Sub cmdGetValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetValue.Click
        If (DataGridView1.SelectedCells.Count = 1) Then
            Dim o As Object = DataGridView1.SelectedCells(0).Value
            'Do what you like.
            MsgBox(o)
        End If
    End Sub
-----------------------EOC-----------------
Good Luck
0
 
clebo99Author Commented:
Thanks....this looks pretty good and I'll give this a try tonight.  A few quick followups:

- One thing I still don't see in the code is where what I selected in the first DataGrid is "appended" to the second select statement on the second form.  

So, my first "form" is going to give me 2 columns.  One with a  computer name and the second with a date of when the system was "added".  I click on the cell with the computer name and I can get that data (I have a MSGBOX that shows that I have grabbed this information correctly).  I then do a simple variablename = "selected cell" (I worked with the dateviewgrid "settings" to get that.  Took me about an hour to figure that out).  

Now, on the "Clicked Cell" action, I show the message box, I close the current form, then I open a second form.  This second form has a datagrid and I've easily been able to get data into that grid AND I've discovered that if I do something like "SELECT * FROM TABLE WHERE NAME = @variablename" in the dataview (I'm sorry I don't have .Net at work so I'm going by memory), a box on the top of the screen comes up and I can type in the system name there and the script works fine.  But, I want the @variablename to what I selected in that original cell that is shown in the MSG box.

- Again, in VB6, what I would do is just use a LISTVIEW and have a WHILE DO using the !variablename and placing that in the "columns" of the list box.  Quite frankly it was my bread and butter for writing applications connecting to DB's and I can still very quickly using that template make viable apps using VB6, but I'm trying to move ahead and do this with .Net 2005.    

Is it easier or a best practice to use a DataGridView or a LISTVIEW for these kinds of designs?  I would assume that a DataGridView would be the obvious choice, but if I am "limited" to only commands in genuine SQL and cannot pass a variable from a form into this statement, it's not much help (I would think this would be an issue for a lot of other programmers as well).

Again, thanks for the code.  Please comment if you think I am off base or even to validate that I'm kind of getting the jist of this.

Chris, Baltimore.
0
 
clebo99Author Commented:
I know....long post by me.....actually I was able to do what I wanted to do using LISTVIEWS, which is what I am more comfortable with anyway using datareaders (my next step is to try datawriters, where I'll probably post a lot about for help).

I'm starting to like .Net more and more (seems like there is a lot more variable definitions required and this datareader/writer is really a big change from Recordsets).  

Thanks again for your help everyone.

chris
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.