dropdown in datagrid

kaisersoze

Centurion
Joined
Aug 27, 2003
Messages
152
Hi all, I have a dropdown in one column of a datagrid. The dropdown is filled from database table. While adding the grid i have no problem, but when i click on edit to change and update, the value in the dropdown is defaulted to the first item in the dropdown. Is there a way i can select the previously selected item, and make the user to change if he wants to.
 
It has to be in there, i've used it before..

I'd say search 4guysfromrolla search. I think i might have found the code there.
 
I know 4Guys has the code..

This is what I did using a sample code from their site:
Code:
<asp:DropDownList runat="server" id="ddlCountry" DataValueField="country_id" DataTextField="country_name" DataSource="<%# GetCountryList() %>" SelectedIndex='<%# GetSelIndex(Container.DataItem("country_id")) %>'

Then
Code:
Function GetCountryList() As DataSet
        'Populate the ddlDataSet and get list of country from country table. This is displayed
        'when user wants to edit the country field on sitedetail page.
        Dim cnn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnString"))
        Dim cmd As SqlCommand = cnn.CreateCommand

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "select country_id, country_name from tablenamestuff"

        Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(cmd.CommandText, ConfigurationSettings.AppSettings("ConnString"))
        myDataAdapter.Fill(ddlDataSet, "Country_master")
        Return ddlDataSet

    End Function

Then

Code:
 Function GetSelIndex(ByVal CountryID As String) As Integer
        'Loop through each row in the DataSet. This is used in dropdownbox for sitedetail.aspx
        'After Edit is clicked, the value in dropdown box stays to what it was , instead of
        'defaulting to the top of the list.(Argentina) in this case.
        Dim iLoop As Integer
        Dim dt As DataTable = ddlDataSet.Tables("Country_master")
        For iLoop = 0 To dt.Rows.Count - 1
            If Int32.Parse(CountryID) = Int32.Parse(dt.Rows(iLoop)("country_id")) Then

                Return iLoop
            End If
        Next iLoop
    End Function

And declare this as global var
Code:
 'global vars
    Dim ddlDataSet As DataSet = New DataSet()
 
Back
Top