Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
Note: I think as a programmer not as a human, so use my answer at your will
Posted

I know 4Guys has the code..

 

This is what I did using a sample code from their site:

<asp:DropDownList runat="server" id="ddlCountry" DataValueField="country_id" DataTextField="country_name" DataSource="<%# GetCountryList() %>" SelectedIndex='<%# GetSelIndex(Container.DataItem("country_id")) %>'

 

Then

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

 

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

'global vars
   Dim ddlDataSet As DataSet = New DataSet()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...