• Data Binding to a drop down list (visual studio 1.0, ASP.NET 1.0)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Data Binding to a drop down list (visual studio 1.0, ASP.NET 1.0)

    Author
    Topic
    #402155

    I have a datagrid which lists people in my database. The datagrid has a Select button on each row. If you click the Select button, you are re-directed to a new web page which displays all the information on the selected person which you can edit. All this is working (thanks to MarkJ).

    Here is my problem: I want to use a drop-down list instead of a text box for some of my input controls on my edit page. When I load my Edit page which just shows one person’s data, how do I get the drop-down list’s default selected item to be the same as what is have stored in the database for that person? For instance, Mr. Bloggs is in the “Great” category in the database. When I open his record on my edit page, how do I get “Great” to show in my Category drop-down list?
    weep

    Viewing 0 reply threads
    Author
    Replies
    • #798022

      Hi Gwenda,

      I definitely lost a bit of hair over learning how to work with Datagrids (despite being a 20-something)! Through that process I learned a few tricks. One of them is to include “hidden” columns in your datagrid that contain values you’ll need for things like this. Since they’re not visible, they stay server-side and never get sent or displayed to the client.

      Simply add an additional Databound column(s) to your datagrid using the Property Builder, and set the Visible property to False. In this case, be sure to select the field that corresponds to the value that you want to assign to your DropDown. For anyone reading this that is not familiar with DropDowns, remember that there are two columns in a dropdown – the Text and the Value. Be sure that this column corresponds to the Value of the Dropdown – not the Text (although sometimes both are the same).

      I’m assuming you’re binding the dropdown to its datasource in the ItemDataBound event of your datagrid – like this:

      Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e  _
        As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
      If e.Item.ItemType = ListItemType.EditItem Then 'If item is in Edit mode
        'Assuming DropDown control is named "myDropDown" and it's in the first cell (0),
        'create a variable to reference the dropdown control in the datagrid
        Dim myDropDown As DropDownList = CType(e.Item.Cells(0).FindControl("myDropDown"), DropDownList)
      
        'Set data source (in this case myDataSet)
        myDropDown.DataSource = myDataSet.Tables(0)
      
        'Bind DataSource
        myDropDown.DataBind()
      
        'Use a Try/Catch in case there is a problem finding the value
        Try
          'Here's the key - set the value to the proper selected item, located in Column 1
          myDropDown.SelectedValue = e.Item.Cells(1).Text
      
        Catch
          'If there is a problem with the value, set it to the first item
          myDropDown.ClearSelection()
        End Try
      End If
      End Sub

      Hope this helps!

      • #798351

        Hi Mark – Thank you so much! My dropdown list is working. I’m used to combo boxes in Access where you can just bind the control to a data source and that’s it. Reading your response, I saw that I actually have to set the default value of the dropdown to match the data.

        I’m not actually using the dropdown in a Datagrid, so I adapted your technique to using a dropdown on my form. Will you take a quick look at my solution below and tell me if there is an easier way – seems kinda cumbersome.

        SOLUTION: I have one data adaptor to read all the data I need on the person. Then a second data adaptor that reads my list of categories. Then I created a ListArray out of the datatable from my second data adaptor. Then I set the default value of the dropdown list using:

        ddlCategory.SelectedIndex = aryCategories.IndexOf(dr(“Category”))

        Where ddlCategory is the dropdown list, aryCategories is the ListArray, and dr is the current data row in my list of people. (I stored the full category text instead of an ID in the people table).

        Thanks Mark. bravo I will not neglect to re-read your post when its time to create a dropdown in a datagrid.

        Oh …. one more quick question … I did not find a “.SelectedValue” property for my dropdown list. Is this because I’m using Web controls? or because I’m using version 1.0?

        • #798466

          Hi Gwenda,

          Last thing first – I should have mentioned that the SelectedValue property was added in the .NET Framework 1.1. You can accomplish the same thing in 1.0 by the following:
          – Set a value using myDropDown.Items.FindByValue(myValue).Selected = True
          Note: You can substitute FindByText(myText) if you are setting the Text as opposed to the Value.
          – Get a value using myDropDown.SelectedItem.Value
          Note: You can substitute .Text if you are looking for the Text instead of the Value

          Ok – it does sound like there is an easier way to accomplish your goal here. Let me offer a suggestion that might work better for you.

          Rather than using multiple DataAdapters, you really only need one. These are very heavy on resources and should be used sparingly.
          Here’s what I would do:

          To Fill your Categories List:
          – Use the Fill method of DataAdapter to fill a DataSet object
          – Bind the DropDown box to the DataSet (ListArray is probably a lighter object, but DataSet is more flexible)
          – Be sure to dispose of the residual objects to free resources

          For your basic form data:
          – Create a Connection
          – Create a Command (set the connection property to the Connection)
          – Create a DataReader
          – Open the Connection
          – Set the Reader to the ExecuteReader method of the Command object
          – Set your field values from the Reader (including the DropDown box)

          There are several other methods as well. I normally use something similar to what I’ve described above, however I encapsulate it in a middle-tier class. This makes life much easier when I need to get the same data from multiple front-end places. Also, I rely very heavily on SQL Stored Procedures to do most of the data processing work. It’s MUCH faster than passing a SQL string as CommandText. If you have SQL Server I highly recommend this.

          Hope this helps!

          • #799466

            Hi Mark – Thank you. First, I’ve got .NET Framework 1.1 working, so I’ll use the syntax you originally suggested.

            Also, it is running REALLY snail slow so I guess its having 3 data adaptors – I’m going to re-work it following your suggestion of using a DataReader for the form. I would love to encapsulate in a class but I think, for now, I’ll be happy just to get this working at a reasonable speed.

          • #799467

            Hi Mark – Thank you. First, I’ve got .NET Framework 1.1 working, so I’ll use the syntax you originally suggested.

            Also, it is running REALLY snail slow so I guess its having 3 data adaptors – I’m going to re-work it following your suggestion of using a DataReader for the form. I would love to encapsulate in a class but I think, for now, I’ll be happy just to get this working at a reasonable speed.

        • #798467

          Hi Gwenda,

          Last thing first – I should have mentioned that the SelectedValue property was added in the .NET Framework 1.1. You can accomplish the same thing in 1.0 by the following:
          – Set a value using myDropDown.Items.FindByValue(myValue).Selected = True
          Note: You can substitute FindByText(myText) if you are setting the Text as opposed to the Value.
          – Get a value using myDropDown.SelectedItem.Value
          Note: You can substitute .Text if you are looking for the Text instead of the Value

          Ok – it does sound like there is an easier way to accomplish your goal here. Let me offer a suggestion that might work better for you.

          Rather than using multiple DataAdapters, you really only need one. These are very heavy on resources and should be used sparingly.
          Here’s what I would do:

          To Fill your Categories List:
          – Use the Fill method of DataAdapter to fill a DataSet object
          – Bind the DropDown box to the DataSet (ListArray is probably a lighter object, but DataSet is more flexible)
          – Be sure to dispose of the residual objects to free resources

          For your basic form data:
          – Create a Connection
          – Create a Command (set the connection property to the Connection)
          – Create a DataReader
          – Open the Connection
          – Set the Reader to the ExecuteReader method of the Command object
          – Set your field values from the Reader (including the DropDown box)

          There are several other methods as well. I normally use something similar to what I’ve described above, however I encapsulate it in a middle-tier class. This makes life much easier when I need to get the same data from multiple front-end places. Also, I rely very heavily on SQL Stored Procedures to do most of the data processing work. It’s MUCH faster than passing a SQL string as CommandText. If you have SQL Server I highly recommend this.

          Hope this helps!

      • #798352

        Hi Mark – Thank you so much! My dropdown list is working. I’m used to combo boxes in Access where you can just bind the control to a data source and that’s it. Reading your response, I saw that I actually have to set the default value of the dropdown to match the data.

        I’m not actually using the dropdown in a Datagrid, so I adapted your technique to using a dropdown on my form. Will you take a quick look at my solution below and tell me if there is an easier way – seems kinda cumbersome.

        SOLUTION: I have one data adaptor to read all the data I need on the person. Then a second data adaptor that reads my list of categories. Then I created a ListArray out of the datatable from my second data adaptor. Then I set the default value of the dropdown list using:

        ddlCategory.SelectedIndex = aryCategories.IndexOf(dr(“Category”))

        Where ddlCategory is the dropdown list, aryCategories is the ListArray, and dr is the current data row in my list of people. (I stored the full category text instead of an ID in the people table).

        Thanks Mark. bravo I will not neglect to re-read your post when its time to create a dropdown in a datagrid.

        Oh …. one more quick question … I did not find a “.SelectedValue” property for my dropdown list. Is this because I’m using Web controls? or because I’m using version 1.0?

    Viewing 0 reply threads
    Reply To: Data Binding to a drop down list (visual studio 1.0, ASP.NET 1.0)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: