0

单击我的 asp.net gridview 上的编辑按钮时出现此错误。

'ddldept' 有一个无效的 SelectedValue,因为它不存在于项目列表中。

这是我的下拉SelectedIndexChanged事件

protected void dgbus_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = dgbus.SelectedRow;

    //here am getting the error 
    ddldept.SelectedValue = row.Cells[2].Text.ToString().Trim();
    txtappcode1.Text = row.Cells[3].Text.ToString();
    txtappcode2.Text = row.Cells[4].Text.ToString();

    hdn.Value = row.Cells[1].Text.ToString();

    lblMsg.Text = "";
}

一切工作正常,但是当我尝试编辑具有row.Cells[2].Text“财务和帐户”的行时..这是“&”有问题吗?

我的 Gridview 标记

<asp:GridView ID="dgbus" runat="server" class="table-format" 
              AutoGenerateColumns="False" Width="100%" 
              OnSelectedIndexChanged="dgbus_SelectedIndexChanged"
              OnRowCreated = "dgbus_RowCreated"
              OnPageIndexChanging = "dgbus_PageIndexChanging" AllowPaging="True" 
              PageSize="50">
     <Columns>
          <asp:CommandField ButtonType="Image" HeaderText="Edit Details" 
                            SelectImageUrl="~/images/modify.gif"
                            SelectText="Modify Approver" 
                            ShowSelectButton="True" />
          <asp:BoundField HeaderText="Approver ID" DataField="appid"/>
          <asp:BoundField HeaderText="Deptt" DataField="deptt" />
          <asp:BoundField HeaderText="Appcode1" DataField="appcode1" />
          <asp:BoundField HeaderText="Appcode1" DataField="appcode2" />
     </Columns>
     <RowStyle  CssClass="misctext" Height="20px" />
     <HeaderStyle BackColor="ControlLight" CssClass="contentbold" 
                  Height="20px" />
     <PagerSettings PageButtonCount="5" />
     <PagerStyle BackColor="Gainsboro" CssClass="link" HorizontalAlign="Right" 
                 VerticalAlign="Middle" />
</asp:GridView>
4

1 回答 1

0

在尝试设置 之前尝试在下拉列表中查找值SelectedValue,如下所示:

if (ddldept.Items.FindByValue(row.Cells[2].Text.ToString().Trim()) != null)
{
    ddldept.SelectedValue = row.Cells[2].Text.ToString().Trim();
}

所以你的完整代码应该是这样的:

protected void dgbus_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = dgbus.SelectedRow;

    if (ddldept.Items.FindByValue(row.Cells[2].Text.ToString().Trim()) != null)
    {
        ddldept.SelectedValue = row.Cells[2].Text.ToString().Trim();
    }        

    txtappcode1.Text = row.Cells[3].Text.ToString();
    txtappcode2.Text = row.Cells[4].Text.ToString();

    hdn.Value = row.Cells[1].Text.ToString();

    lblMsg.Text = "";
}
于 2013-11-02T12:40:32.263 回答