简体   繁体   English

将选定的Gridview行数据放入数据绑定下拉列表

[英]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: 我使用此代码将选定的gridview行放入Label或文本框,并且它们工作正常,但是,当我选择在从sqldatasource加载数据的下拉列表中显示数据时,会产生此错误:

'ddlRPGroup' has a SelectedValue which is invalid because it does not exist in the list of items. 'ddlRPGroup'的SelectedValue无效,因为它在项目列表中不存在。

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. 该错误告诉您,要在DropdownList中选择的值尚未存在于列表中。

If this value already should be in the ddl, then you need to edit the DataSource of your ddl to insert the correct values. 如果此值已经在ddl中,则需要编辑ddl的数据源以插入正确的值。 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 它应该是selectItem.Text,因为selectedValue指向项目的代码,而不是项目本身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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