kaisersoze Posted September 10, 2004 Posted September 10, 2004 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. Quote Note: I think as a programmer not as a human, so use my answer at your will
eramgarden Posted September 11, 2004 Posted September 11, 2004 I think i did something like this from an example on this site: http://aspnet.4guysfromrolla.com/articles/040502-1.aspx Quote
kaisersoze Posted September 12, 2004 Author Posted September 12, 2004 I did not find. can anyone help me please. I did not find. can anyone help me please. Quote Note: I think as a programmer not as a human, so use my answer at your will
eramgarden Posted September 13, 2004 Posted September 13, 2004 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. Quote
eramgarden Posted September 13, 2004 Posted September 13, 2004 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() Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.