Simple DataGridView Question - Passing Variables

Posted on 2007-07-29
Last Modified: 2012-05-05
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.
Question by:clebo99
    LVL 11

    Assisted Solution

    Heres a quick example to populate in code and grab the selected value when only one is selected.
        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

                Catch ex As Exception
                    'cLog.LogError(ex, sSQL , True)
                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
        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.
            End If
        End Sub
    Good Luck

    Author Comment

    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.
    LVL 11

    Accepted Solution

    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 & 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. )
    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.

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 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

    12 Experts available now in Live!

    Get 1:1 Help Now