0

hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. 最后一个字段是一个下拉列表,单击 gridview 时它不显示。我设置了一个断点,发现它卡在第一个 - 0 索引上。我不知道为什么它没有向前发展......这是代码:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    ....

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



<asp:DropDownList ID="DropDownListCurrency" runat="server" 
        CausesValidation="True"     
        DataSourceID="CurrencyDropDownListDataSource" 
        DataTextField="Currency" DataValueField="Currency_ID"
        AppendDataBoundItems="True">
    <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
4

4 回答 4

1

为什么要从文本框中获取值。会更好地在该事件中使用这样的 DataKeyNames

GridViewRow row = GridView.SelectedRow;

int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);

如果您在 DataKeyName 中只有一个值,则此工作如果您看不到索引,如果您想要多个值,请使用此

int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);

string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
于 2013-11-12T13:48:09.457 回答
0

DropDownList 是否包含您要显示的单词?

于 2013-11-12T13:48:16.270 回答
0

您需要将AutoPostBack属性设置为True

它应该是 :

<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server" 
        CausesValidation="True"     
        DataSourceID="CurrencyDropDownListDataSource" 
        DataTextField="Currency" DataValueField="Currency_ID"
        AppendDataBoundItems="True">
    <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
于 2013-11-12T13:54:37.583 回答
0

犯了一个简单的错误...

称为错误的字段应该是值(Currency_ID)而不是文本(Currency)

DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
于 2013-11-12T14:35:34.897 回答