Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid DropDown - OnSelectedIndexChanged

Status
Not open for further replies.

BoydMT

Programmer
May 28, 2003
32
US
I have a datagrid with a number of fields. Among them are [CompanyName] and [CompanyType]. In Edit mode, [CompanyName] is a drop-down list, and [CompanyType] remains a label.
Whenever the value in [CompanyName] is changed, I need to search a table for the appropriate company type, and change the [CompanyType] value.
I know that I need to use the OnSelectedIndexChanged function of the [CompanyName] drop-down, but I am having trouble with the code.
Has anyone done this type of thing before, or can you point me in the right direction?

Thanks,
BoydM
 
Thanks for the response. I've come across this example in the past, but am only familiar with VB.net, not C#.
After working with my issue over the past few hours, I'm now able to query the table for the appropriate company type. My issue now is passing this value to the field in my datagrid. I want to do this immediately after the user selects the company. Do you know the proper syntax for passing a value to a datagrid column?

Thanks,
BoydMT
 
Hi BoydMT

I am also coding in VB.Net, it is not that difficult to translate the example to VB.Net, just understand what the example is trying to do.

Anyway, I believe this what you looking for. With the html codes, the example shows this.
<EditItemTemplate>
<asp:DropDownList id="ddl" runat="server" Width="300px" DataValueField="company" DataTextField="company" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectIndexChanged"></asp:DropDownList></EditItemTemplate>

The example shows us to create a function for OnSelectedIndexChanged event for ddl (Dropdown list) in the code-behind also.

Public Sub ddl_SelectIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim dgi As DataGridItem = CType(ddl.Parent.Parent, DataGridItem)
Dim ddlAddress As DropDownList = CType(dgi.FindControl("ddlAddress"), DropDownList)

FillAddress(ddl.SelectedValue, ddlAddress, "")
End Sub

Then you can change the other ddl based on the selected value of the first ddl. For my case, I change my address on selection of my company.

Private Shared Sub FillAddress(ByVal company As String, ByRef ddlAddress As DropDownList, ByVal address As String)
Dim DataSystem As CompanyDB = New CompanyDB
ddlAddress.DataSource = DataSystem.GetAllAddress(company)
ddlAddress.DataBind()

Dim lstItem As ListItem = ddlAddress.Items.FindByValue(address)
If Not (lstItem Is Nothing) Then
lstItem.Selected = True
End If
End Sub


ke5146
 
Thanks. This seems to be what I'm looking for.
One question, though. In the statement 'Dim DataSystem As CompanyDB = New CompanyDB', what exactly is CompanyDB? Do I need to import it somewhere?

Thanks again,
BoydMT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top