Two DropDownList controls in one DataGrid cell

G

Guest

Hi folks,

I need to have two ddl's in one DataGrid cell. The first ddl has a list of
company names and the second has a list of contact names that work in the
company name that was selected in the first ddl.

Any replies will be greatly appreciate.

Here are 3 functions / subroutines that do the following:

1) GetCompanyNamesForThisProject
2) GetContactsFromCompanyForThisProject
3) ddlTo_SelectedIndexChanged


Function GetCompanyNamesForThisProject() as DataSet
dsCompanyNames.Clear()
Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;
Ole DB Services=-4; Data
Source=c:\sites\single29\gmeadows73\database\fdmdb.mdb"
Dim queryString As String = "SELECT DISTINCT [users].[cname] FROM
[users] WHERE ([users].[project] = '" & Session("project") & "')"

Dim dataAdapter As New OleDbDataAdapter (querystring, strConnString)

dataAdapter.Fill(dsCompanyNames, "users

Return dsCompanyNames
End Function

Function GetContactsFromCompanyForThisProject () As DataSet
' dsContacts.Clear()
Dim strConnString As String =
"Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
Source=C:\sites\single29\gmeadows73\database\fdmdb.mdb"

Dim queryString As String
queryString = "SELECT [users].[name] FROM [users] WHERE
(([users].[cname] = '" & coName & "') AND ([users].[project] = '" &
Session("project") & "'))"

Dim dataAdapter As New OleDbDataAdapter (queryString,
strConnString)

dataAdapter.Fill(dsContacts, "users")

Return dsContacts
End Function

Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim list As DropDownList = CType(sender, DropDownList)
coName = list.SelectedItem.Text

Dim ddlContact As DropDownList = CType(sender, DropDownList)
ddlContact.DataSource = GetContactsFromCompanyForThisProject ()
ddlContact.DataBind ( )
End Sub

The first ddl is populated with company names. When it is selected the code
travels into the second function to get the contact names for the company
that was selected. The problem is that the second ddl is never populated
with the contact names.

My DataGrid html snippet is as follows:

<asp:TemplateColumn HeaderText="To"
SortExpression="questionto">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"questionto") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="lblFrom" text="Company: "
runat="server" />
<br />
<asp:DropDownList id="ddlTo" runat="server"
DataValueField="cname" AutoPostBack="True" DataSource='<%#
GetCompanyNamesForThisProject() %>'
OnSelectedIndexChanged="ddlTo_SelectedIndexChanged" />
<asp:Label id="lblContact" text="Contact: "
runat="server" />
<br />
<asp:DropDownList id="ddlContact" runat="server"
DataValueField="name" AutoPostBack="True" DataSource='<%#
GetContactsFromCompanyForThisProject() %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
 
G

Guest

Hi folks,

Solved my own problem. For anyone interested, the following snippet needs
to go inside of your OnSelectItem subroutine:

Dim list As DropDownList = CType(sender, DropDownList)
coName = list.SelectedItem.Text

Dim cell As TableCell = CType(list.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
Dim ddlContact As DropDownList =
CType(item.FindControl("ddlContact"), DropDownList)
ddlContact.DataSource = GetContactsFromCompanyForThisProject ()
ddlContact.DataBind ( )



glenn said:
Hi folks,

I need to have two ddl's in one DataGrid cell. The first ddl has a list of
company names and the second has a list of contact names that work in the
company name that was selected in the first ddl.

Any replies will be greatly appreciate.

Here are 3 functions / subroutines that do the following:

1) GetCompanyNamesForThisProject
2) GetContactsFromCompanyForThisProject
3) ddlTo_SelectedIndexChanged


Function GetCompanyNamesForThisProject() as DataSet
dsCompanyNames.Clear()
Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;
Ole DB Services=-4; Data
Source=c:\sites\single29\gmeadows73\database\fdmdb.mdb"
Dim queryString As String = "SELECT DISTINCT [users].[cname] FROM
[users] WHERE ([users].[project] = '" & Session("project") & "')"

Dim dataAdapter As New OleDbDataAdapter (querystring, strConnString)

dataAdapter.Fill(dsCompanyNames, "users

Return dsCompanyNames
End Function

Function GetContactsFromCompanyForThisProject () As DataSet
' dsContacts.Clear()
Dim strConnString As String =
"Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
Source=C:\sites\single29\gmeadows73\database\fdmdb.mdb"

Dim queryString As String
queryString = "SELECT [users].[name] FROM [users] WHERE
(([users].[cname] = '" & coName & "') AND ([users].[project] = '" &
Session("project") & "'))"

Dim dataAdapter As New OleDbDataAdapter (queryString,
strConnString)

dataAdapter.Fill(dsContacts, "users")

Return dsContacts
End Function

Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim list As DropDownList = CType(sender, DropDownList)
coName = list.SelectedItem.Text

Dim ddlContact As DropDownList = CType(sender, DropDownList)
ddlContact.DataSource = GetContactsFromCompanyForThisProject ()
ddlContact.DataBind ( )
End Sub

The first ddl is populated with company names. When it is selected the code
travels into the second function to get the contact names for the company
that was selected. The problem is that the second ddl is never populated
with the contact names.

My DataGrid html snippet is as follows:

<asp:TemplateColumn HeaderText="To"
SortExpression="questionto">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"questionto") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id="lblFrom" text="Company: "
runat="server" />
<br />
<asp:DropDownList id="ddlTo" runat="server"
DataValueField="cname" AutoPostBack="True" DataSource='<%#
GetCompanyNamesForThisProject() %>'
OnSelectedIndexChanged="ddlTo_SelectedIndexChanged" />
<asp:Label id="lblContact" text="Contact: "
runat="server" />
<br />
<asp:DropDownList id="ddlContact" runat="server"
DataValueField="name" AutoPostBack="True" DataSource='<%#
GetContactsFromCompanyForThisProject() %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top