Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Simple DataGridView Question - Passing Variables

Posted on 2007-07-29
Medium Priority
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
  • 2
  • 2
LVL 11

Assisted Solution

coopzz earned 2000 total points
ID: 19590256
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

ID: 19591679
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

coopzz earned 2000 total points
ID: 19605600
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.

Author Comment

ID: 19607937
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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

579 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