简体   繁体   中英

Put Selected Gridview Row Data to a Databound Dropdownlist

I use this code to put the selected gridview row to Label or textbox and they are working properly, However when I choose to display the data in a dropdownlist which loads data from sqldatasource, it produces this error:

'ddlRPGroup' has a SelectedValue which is invalid because it does not exist in the list of items.

protected void grdTenant_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = grdTenant.SelectedRow;
    lblRPCode.Text  = row.Cells[1].Text;
    lblRP.Text   = row.Cells[2].Text;
    lblType.Text = row.Cells[3].Text;
    lblBusiness.Text  = row.Cells[4].Text;

    ddlRPGroup.SelectedValue = row.Cells[5].Text;
}

Markup

<table border="0" class="tblEditTenant"  cellpadding="3">
     <tr>
        <td colspan ="4" style="font-weight: 700; font-size: medium;"><asp:Label ID="lblRP" runat="server" Text="Retail Partner"></asp:Label>- 
        <asp:Label ID="lblRPCode" runat="server" Text="RP Code"></asp:Label>  </td>                         
     </tr>
     <tr>
       <tr>
          <td>Type:</td>
          <td colspan ="3"><asp:Label ID="lblType" runat="server" Text="Type"></asp:Label></td>
       </tr>
      <tr>
          <td>Business:</td>
          <td colspan ="3"><asp:Label ID="lblBusiness" runat="server" Text="Business"></asp:Label></td>
      </tr>
      <tr>
          <td>RP Group:</td>
          <td colspan ="3">
               <asp:DropDownList ID="ddlRPGroup" runat="server" DataSourceID="SqlDataSource3" DataTextField="name" DataValueField="code"></asp:DropDownList>&nbsp;<asp:DropDownList ID="DropDownList1" runat="server">
               </asp:DropDownList>
          </td>                             
      </tr>
</table>

What the error tells you, that the value you want to select in your DropdownList does not yet exist in the list.

If this value already should be in the ddl, then you need to edit the DataSource of your ddl to insert the correct values. If you want to insert the selected value into the dropdownlist first and then select it, do it like this:

protected void grdTenant_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = grdTenant.SelectedRow;
    lblRPCode.Text  = row.Cells[1].Text;
    lblRP.Text   = row.Cells[2].Text;
    lblType.Text = row.Cells[3].Text;
    lblBusiness.Text  = row.Cells[4].Text;

    //new ListItem with Text and Value of cells[5] gets inserted into ddl
    ddlRPGroup.Items.Insert(0, new ListItem(row.Cells[5].Text,row.Cells[5].Text));
    ddlRPGroup.SelectedValue = row.Cells[5].Text;
}

If this value should already be in the list, the you need to edit your datasource to return to proper values.

Found out the answer already, and what is causing the error.

Just replace this line

ddlRPGroup.SelectedValue = row.Cells[5].Text;

with this:

ddlRPGroup.SelectedItem.Text = row.Cells[5].Text;

It should be selectItem.Text because selectedValue points the code of the item and not the item itself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM