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>
<aspropDownList
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>
<aspropDownList
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?
GridView with the following code:
``````````````````````````````````````````````````````
<asp:GridView id="gvGridView" runat="server"
AutoGenerateColumns="False" DataKeyNames="idRecordID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<aspropDownList
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>
<aspropDownList
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?