title: c# drop down list in grid view
body: <p>I am a grid view that I have that I can edit, update, delete from.  I can manually add a drop down and have the selected value populate correctly, but I want to be able to populate the drop down from the database (and show the selected value correctly) when editing it.</p>
  
  <p>I have tried a bunch of searches/options but can not find a way to make it work.</p>
  
  <p>My code is below:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              PopulateData();
              lblMessage.Text = "";
          }
  
          string sSQL = "";
  
          // populate drop downs
          sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
              FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
              WHERE ErrorType = 'Category'
              ";
          //ORDER BY OrderBy, ErrorDescription";
  
          new DatabaseConnection().PopulateListBoxFromDB(sSQL, "", lstNewResolutionCategory);
  
  
          sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
              FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
              WHERE ErrorType = 'Severity'
              ";
          //ORDER BY OrderBy, ErrorDescription";
  
          new DatabaseConnection().PopulateListBoxFromDB(sSQL, "", lstNewResolutionSeverity);
  
  
      }//end page load
  
  
      protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
      {
  
          var ResolutionsID = GridView1.DataKeys[e.RowIndex].Value;
  
          GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
  
          string sSQL = "Delete from BabelFish.dbo.Resolutions where ResolutionsID = @ResolutionsID";
  
          SqlCommand sCommand = new SqlCommand(sSQL);
  
          sCommand.Parameters.AddWithValue("@ResolutionsID", ResolutionsID);
  
          // run delete
          new DatabaseConnection().RSExecute(sCommand);
  
          lblMessage.Text = "Record deleted successfully !";
  
          GridView1.EditIndex = -1;
  
          this.PopulateData();
  
      }
  
  
  
  
      protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
      {
  
          var ResolutionsID = GridView1.DataKeys[e.RowIndex].Value;
  
          GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
  
          TextBox txtResolutionDescription = row.FindControl("txtResolutionDescription") as TextBox;
          DropDownList drpErrorCategoryID = row.FindControl("ErrorCategory") as DropDownList;
  
  
          string sSQL = @"Update BabelFish.dbo.Resolutions set 
                      ResolutionDescription = @ResolutionDescription,
                      UserIDSolved = ISNULL(BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(@UserIDSolved), BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(REPLACE(@UserIDSolved, '-', ''))), 
                      DateTimeSolved = ISNULL(DateTimeSolved, GetDate())                        
                      where ResolutionsID = @ResolutionsID";
  
          SqlCommand sCommand = new SqlCommand(sSQL);
  
          sCommand.Parameters.AddWithValue("@ResolutionDescription", txtResolutionDescription.Text.Trim());
          sCommand.Parameters.AddWithValue("@ResolutionsID", ResolutionsID);
          sCommand.Parameters.AddWithValue("@UserIDSolved", UserID);
  
          // run update
          new DatabaseConnection().RSExecute(sCommand);
  
  
          lblMessage.Text = "Record updated successfully !";
  
          GridView1.EditIndex = -1;
  
          this.PopulateData();
  
      }
  
  
  
  
      private void PopulateData()
      {
          string sSQL = @"SELECT
                  ResolutionsID, ErrorTableID, BabelFish.dbo.fn_GetUserNameFromTeamMemberTable(UserIDSolved) as UserIDSolved, 
                  DateTimeSolved, ResolutionDescription, ResolutionCategory, ResolutionSeverity, IsActive
                  FROM BabelFish.dbo.Resolutions (NOLOCK)
                  Where ErrorTableID = '" + ErrorTableID + "'";
  
          DataTable dt = DatabaseAccessing.DatabaseConnection.GetDataTable(sSQL);
  
          // only do if more then 1 row exists
          if (dt.Rows.Count &gt; 0)
          {
              GridView1.DataSource = dt;
  
              GridView1.DataBind();
          }
          else
          {
              lblMessage.Text = "No Rows Exist.";
          }
  
  
  
  
      }
  
  
      protected void AddRow(object sender, EventArgs e)
      {
          // get values to add to database
          string txtResolutionDescription = txtNewResolutionDescription.Text.ToString();
  
          string lstCategoryID = lstNewResolutionCategory.SelectedValue;
          string lstSeverityID = lstNewResolutionSeverity.SelectedValue;
  
          string sSQL = @"INSERT INTO BabelFish.dbo.Resolutions (
              ErrorTableID, UserIDSolved, DateTimeSolved, ResolutionDescription, 
              ResolutionCategory, ResolutionSeverity, IsActive
          )
  
          VALUES (
              @ErrorTableID, ISNULL(BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(@UserIDSolved), BabelFish.dbo.fn_GetUserIDFromTeamMemberTable(REPLACE(@UserIDSolved, '-', ''))), 
              GetDate(), @ResolutionDescription,
              @ResolutionCategory, @ResolutionSeverity, 1
          )";
  
  
          SqlCommand sCommand = new SqlCommand(sSQL);
  
          sCommand.Parameters.AddWithValue("@ErrorTableID", ErrorTableID);
          sCommand.Parameters.AddWithValue("@UserIDSolved", UserID);
          sCommand.Parameters.AddWithValue("@ResolutionDescription", txtResolutionDescription);
          sCommand.Parameters.AddWithValue("@ResolutionCategory", lstCategoryID);
          sCommand.Parameters.AddWithValue("@ResolutionSeverity", lstSeverityID);
  
  
          // run update
          new DatabaseConnection().RSExecute(sCommand);
  
  
          lblMessage.Text = "Record successfully added!";
  
          GridView1.EditIndex = -1;
  
          this.PopulateData();
      }
  
  
      protected void EditRow(object sender, GridViewEditEventArgs e)
      {
          GridView1.EditIndex = e.NewEditIndex;
  
          this.PopulateData();
  
          /*
          //not working, ddl is NULL
          var ddl = (DropDownList)GridView1.FindControl("selResolutionSeverity");
  
          string sSQL = @"SELECT ErrorTypeLookupID as Value, ErrorDescription as DisplayText
              FROM BabelFish.dbo.ErrorTypeLookup (NOLOCK)
              WHERE ErrorType = 'Severity' ";
  
  
          DataSet DS = new DatabaseAccessing.DatabaseConnection().DS(sSQL);
  
          ddl.DataSource = DS;
          ddl.DataTextField = "DisplayText";
          ddl.DataValueField = "Value";
          ddl.DataBind();
          ddl.Items.Insert(0, new ListItem("-- Select --", "0"));
           * */
      }
  
  
  
      protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)
      {
          GridView1.EditIndex = -1;
          this.PopulateData();
      }
  
  
      protected void ChangePage(object sender, GridViewPageEventArgs e)
      {
  
          GridView1.PageIndex = e.NewPageIndex;
          this.PopulateData();
  
      }
  
  
  
      &lt;asp:Label ID="lblMessage" runat="server" ForeColor="Green" EnableViewState="false" /&gt;
  
      &lt;asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
          AutoGenerateColumns="false" 
          Width="100%" 
          OnRowEditing="EditRow" 
          OnRowCancelingEdit="CancelEditRow"
          OnRowUpdating="UpdateRow" 
          DataKeyNames="ResolutionsID" 
          OnRowDeleting="DeleteRow" 
          AllowPaging="true"
          PageSize="50" 
          OnPageIndexChanging="ChangePage"            
  
          &gt;
  
      &lt;Columns&gt;
  
          &lt;asp:TemplateField HeaderText="Edit"&gt;
  
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" /&gt;
              &lt;/ItemTemplate&gt;
  
              &lt;EditItemTemplate&gt;
  
                  &lt;asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" /&gt;
  
                  &lt;asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" /&gt;
  
              &lt;/EditItemTemplate&gt;
  
          &lt;/asp:TemplateField&gt;
  
  
          &lt;asp:BoundField HeaderText="ResolutionsID" DataField="ResolutionsID" ReadOnly="true" /&gt;
  
          &lt;asp:TemplateField HeaderText="ResolutionDescription"&gt;
              &lt;ItemTemplate&gt;&lt;%# Eval("ResolutionDescription")%&gt;&lt;/ItemTemplate&gt;
  
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="txtResolutionDescription" runat="server" Text='&lt;%# Eval("ResolutionDescription") %&gt;'/&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:BoundField HeaderText="UserIDSolved" DataField="UserIDSolved" ReadOnly="true" /&gt;
  
          &lt;asp:TemplateField HeaderText="Category"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("ResolutionCategory")%&gt;
              &lt;/ItemTemplate&gt;
  
              &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                  &lt;EditItemTemplate&gt;  
                      &lt;asp:DropDownList ID="selResolutionCategory" runat="server" SelectedValue='&lt;%# Eval("ResolutionCategory") %&gt;'&gt;         
                          &lt;asp:ListItem Text="-- Select One --" Value="0" /&gt;
                          &lt;asp:ListItem Text="cat1" Value="1" /&gt;
                          &lt;asp:ListItem Text="cat2" Value="2" /&gt;        
                      &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
  
          &lt;asp:TemplateField HeaderText="Severity"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("ResolutionSeverity")%&gt;
              &lt;/ItemTemplate&gt;
  
              &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                  &lt;EditItemTemplate&gt;  
                      &lt;asp:DropDownList ID="selResolutionSeverity" runat="server"&gt;                                     
                      &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
  
          &lt;asp:TemplateField HeaderText="Delete?"&gt;
              &lt;ItemTemplate&gt;
                  &lt;span onclick="return confirm('Are you sure to delete?')"&gt;
                      &lt;asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete" /&gt;
                  &lt;/span&gt;
              &lt;/ItemTemplate&gt;
  
          &lt;/asp:TemplateField&gt; 
  
  
      &lt;/Columns&gt;
  
      &lt;AlternatingRowStyle BackColor="White" /&gt;
  
      &lt;EditRowStyle BackColor="#efefef" /&gt;
  
      &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
  
      &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
  
      &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
  
      &lt;RowStyle BackColor="#EFF3FB" /&gt;
  
      &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
  
      &lt;SortedAscendingCellStyle BackColor="#F5F7FB" /&gt;
  
      &lt;SortedAscendingHeaderStyle BackColor="#6D95E1" /&gt;
  
      &lt;SortedDescendingCellStyle BackColor="#E9EBEF" /&gt;
  
      &lt;SortedDescendingHeaderStyle BackColor="#4870BE" /&gt;
  
      &lt;/asp:GridView&gt;
  </code></pre>