vb net visual studio how to pass fields from a gridview into a sqldatasource from a selected row

Posted on 2009-04-22
Last Modified: 2012-06-21
Hi all,

Im after some guidence.

I have a gridview and one of the columns is a select column which highlights the row. I want to pass some of the selected fields into a sql datasource. Ultimately the result of the sql datasource will then go into another gridview to show further details of the original selected gridview row.
Question by:9772885
    1 Comment
    LVL 5

    Accepted Solution

    You can get the value of any cell in the selected row by doing this:

    string theValue = gridview..SelectedRow.Cells[0].Text;

    Where the zero indicates the cell's index, so zero would be the first cell, one would be the second cell, etc.

    Here is an easy way to do what you need:

    You can store that value in a hidden label on the page, and then on your sql datasource (in design view) click on the datasource and go to its properties, it should have a "where" property, with an elipse button to the right (the one with the three dots) - click that button. From there you can set up a parameter for the datasource which gets its value from the label (which you set earlier).

    Here is an example where clause that you would use:

    it.productID = @productID

    "it" refer's to the table that is selected in your datasource, followed by the column that you want to check. So in the example above you would have a table with the foreign key being "productID". "@productID" is the parameter name, yes the @ symbol is needed and tells the code that its a parameter. This only tells the datasource that there is a parameter called productID, but we now need to create that parameter and give it a value.

    Now below that there should be an area for parameters, with an Add Parameter button - click the add button, and call the parameter "productID". Then on the right hand side, you need to specify where this parameter is getting its value from - which in this case would be the label that you placed on your page. So in the Parameter Source drop down, choose "Control", and from the Control ID drop down, you would choose the label control that you placed.

    The last step is to specify the datatype of this parameter. Click on the "show advanced properties" link. From the "DBType" drop down, you need to choose the datatype that the "productID" column is set to in the database. So eg. if your productID is a Guid, then you need to choose Guid from this drop down.

    Once that is done you can click on Ok, and it should work.

    Featured Post

    What Security Threats Are You Missing?

    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.

    Join & Write a Comment

    Using Quotation Marks in PHP This question ( seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
    If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

    754 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

    20 Experts available now in Live!

    Get 1:1 Help Now