Specifying a DataBound DropDownList in the EditItemTemplate with a Properly Set SelectedIndex

We are getting even closer to our end goal! Now the DropDownList's default value corresponds to the value of the row being edited! Compare this to the output of the previous example.

 FAQ IDCategoryFAQ Question
Edit2 Strings How do you convert a string to all uppercase or all lowercase?
Edit12 Email How can I display line breaks in an email message?
Edit14 Email Can I send emails without using CDONTS?
Edit15 Strings How can I determine the length of a string (how many characters are in a string)?
Edit17 Strings How can I find the position of a certain character (or substring) within a string?
Edit19 Dates and Times How can I return the current date/time?
Edit20 Functions and Subroutines What is recursion?
Edit23 FileSystemObject What, exactly, is the FileSystemObject?
Edit25 Email How can I send an email using CDONTS?
Edit8 Databases, General How do you create a database table through a SQL statement (via an ASP page, perhaps)?
Edit16 Dates and Times Using VBScript, how can I determine if a variable is a valid date or not?
Edit18 Dates and Times How can I easily display a nicely formatted date variable?
Edit24 FileSystemObject How can I read the contents of a text file through an ASP page?
Edit7 Arrays How do I dynamically resize an array?
Edit11 Email How can I have my ASP page send HTML-formatted email?
Edit10 Databases, General How can I tell what version of ADO I am using?
Edit13 Email How do you send email attachments through an ASP page?
Edit21 Arrays How can I determine the upper or lower bounds of an array?
Edit22 Email How can I validate an email adress using Jscript?

Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  'Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
  Dim ddlDataSet as DataSet = New DataSet()

  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
    End If
  End Sub

  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT FAQID, F.FAQCategoryID, F.Description, FC.Name AS CategoryName FROM tblFAQ F INNER JOIN tblFAQCategory FC ON F.FAQCategoryID = FC.FAQCategoryID WHERE FAQID <= 25"

    'Set the datagrid's datasource to the datareader and databind
    Dim resultsDataSet as New DataSet()
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection) 

    dgPopularFAQs.DataSource = resultsDataSet
  End Sub

  Function GetCategories() as DataSet
    'Populate the ddlDataSet
    Const strSQLDDL as String = _
         "SELECT FAQCategoryID, Name FROM tblFAQCategory ORDER BY Name"    
    Dim myDataAdapter as SqlDataAdapter = New _
                          SqlDataAdapter(strSQLDDL, myConnection)    
    myDataAdapter.Fill(ddlDataSet, "Categories")

    Return ddlDataSet
  End Function

  Function GetSelectedIndex(CID as String) as Integer
    Dim iLoop as Integer
    Dim dt as DataTable = ddlDataSet.Tables("Categories")
    For iLoop = 0 to dt.Rows.Count - 1
      If Int32.Parse(CID) = Int32.Parse(dt.Rows(iLoop)("FAQCategoryID")) then
        Return iLoop
      End If
    Next iLoop
  End Function

  Sub dgPopFAQs_Edit(sender As Object, e As DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = e.Item.ItemIndex
  End Sub

  Sub dgPopFAQs_Cancel(sender As Object, e As DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = -1
  End Sub

  Sub dgPopFAQs_Update(sender As Object, e As DataGridCommandEventArgs)
    Response.Write("<b>The Update Command has no effect on this live demo...</b>")
    dgPopularFAQs.EditItemIndex = -1
  End Sub


    <asp:datagrid id="dgPopularFAQs" runat="server"
	    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="OK" />
	    <asp:BoundColumn DataField="FAQID" ItemStyle-Width="10%" ReadOnly="True"
				ItemStyle-HorizontalAlign="Center" HeaderText="FAQ ID" />
	    <asp:TemplateColumn HeaderText="Category">
            <%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
	        <asp:DropDownList runat="server" id="lstCategories"
	              DataValueField="FAQCategoryID"  DataTextField="Name"
	              DataSource='<%# GetCategories() %>' 
	              SelectedIndex='<%# GetSelectedIndex(Container.DataItem("FAQCategoryID")) %>' />

	    <asp:BoundColumn DataField="Description" HeaderText="FAQ Question" />	  

[Return to the article]