Passing a Parameter to DropDownList DataSource Call within GridView Row

D

dawg1998

I am able to populate a DropDownList control within multiple rows of a
GridView with the following code:

``````````````````````````````````````````````````````
<asp:GridView id="gvGridView" runat="server"
AutoGenerateColumns="False" DataKeyNames="idRecordID">
<Columns>
<asp:TemplateField>
<ItemTemplate>

<asp:DropDownList
id="ddlDropDownList1"
runat="server"
DataTextField="strName"
DataValueField="intValue"
DataSource='<%# LoadDDL() %>'
SelectedValue='<%# Bind("intRowValue") %>' />

</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="intRowValue" HeaderText="RowValue" />
</Columns>
</asp:GridView>

CodeBehind:

Function LoadDDL() As DataSet

Dim objConn As New SqlConnection()
Dim objDataSet As New DataSet()
Dim strSQL As String
objConn = New
SqlConnection(ConfigurationManager.ConnectionStrings("connDatabaseConnection").ConnectionString)

strSQL = "SELECT strName, intValue FROM tblTable;"

Dim objDataAdapter As New SqlDataAdapter(strSQL, objConn)
objDataAdapter.Fill(objDataSet, "tblTable")
Return objDataSet

End Function

``````````````````````````````````````````````

But now I want to populate the dropdownlist dynamically with a variable
that comes from the record of the Gridivew row itself, like this:

`````````````````````````````````````````

<asp:GridView id="gvGridView" runat="server"
AutoGenerateColumns="False" DataKeyNames="idRecordID">
<Columns>
<asp:TemplateField>
<ItemTemplate>

<asp:DropDownList
id="ddlDropDownList1"
runat="server"
DataTextField="strName"
DataValueField="intValue"
DataSource='<%# LoadDDL(idRecordID) %>'
SelectedValue='<%# Bind("intRowValue") %>' />

</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="intRowValue" HeaderText="RowValue" />
</Columns>
</asp:GridView>

CodeBehind:

Function LoadDDL(ByVal idRecordID As Integer) As DataSet

Dim objConn As New SqlConnection()
Dim objDataSet As New DataSet()
Dim strSQL As String
objConn = New
SqlConnection(ConfigurationManager.ConnectionStrings("connDatabaseConnection").ConnectionString)

strSQL = "SELECT strName, intValue FROM tblTable WHERE intValue = "
& idRecordID & ";"

Dim objDataAdapter As New SqlDataAdapter(strSQL, objConn)
objDataAdapter.Fill(objDataSet, "tblTable")
Return objDataSet

End Function

`````````````````````````````````````````````

This second version doesn't work. The problem is, I can't figure out
how to pass a value from the row (or DataKeyNames) into the DataSet
call in the DropDownList Datasource property. Is this possible and does
anyone know the syntax for passing a parameter this way?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top