• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now