如何在Gridview中设置dropdownlist选择值?

时间:2015-10-13 11:28:36

标签: c# asp.net gridview dropdown

我在网格视图中有一个下拉列表。这是gridview

<asp:GridView ID="gvFabricDetails" runat="server" AutoGenerateColumns="False" ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" Width="49px" onrowdatabound="gvFabricDetails_RowDataBound" >
    <Columns>
        <asp:TemplateField HeaderText="Programme">
            <ItemTemplate>
                <asp:DropDownList ID="ddlProgramme" DataTextField="PROGRAMME_NAME" DataValueField="PROGRAMME_ID" SelectedValue='<%# Bind("PROGRAMME") %>' AppendDataBoundItems="true"  runat="server"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Lot/Style">
            <ItemTemplate>
                <asp:TextBox ID="txtLot" TabIndex="2" Text='<%# Bind("LOT_STYLE") %>' runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Art#">
        <ItemTemplate>
            <asp:TextBox ID="txtArt" TabIndex="3" Text='<%# Bind("ART") %>' runat="server" ></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

这是绑定数据的代码

public DataTable loadFabricInfos()
{
    DataTable dt = new DataTable();
    string sql = "";

    sql = "SELECT PROGRAMME,LOT_STYLE,ART FROM FABRICISSUE";

    OracleCommand objCommand = new OracleCommand(sql);
    OracleDataAdapter objDataAdapter = new OracleDataAdapter(objCommand);
    using (OracleConnection strConn = GetConnection())
    {
        try
        {
            objCommand.Connection = strConn;
            strConn.Open();
            objDataAdapter.Fill(dt);
        }

        catch (Exception ex)
        {
            throw new Exception("Error : " + ex.Message);
        }

        finally
        {
            strConn.Close();
        }
    }

    return dt;
}

现在问题是gridview中的其他文本框正在获取数据。但是下拉列表不与数据库数据绑定。 SelectedValue属性给出了如下错误:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'PROGRAMME'.

2 个答案:

答案 0 :(得分:0)

更改此行 您的查询是SELECT PROGRAM LOT_STYLE ART FROM FABRICISSUE

您应该使用您的查询字段

DataTextField =“ LOT_STYLE ”DataValueField =“ PROGRAM ”SelectedValue ='&lt;%#Bind(“ PROGRAM ”)%&gt; “

<asp:DropDownList ID="ddlProgramme" DataTextField="PROGRAMME_NAME" DataValueField="PROGRAMME" SelectedValue='<%# Bind("PROGRAMME") %>' AppendDataBoundItems="true"  runat="server"></asp:DropDownList>

答案 1 :(得分:0)

您可以将其绑定到后面代码中的radgrid Itemdatabond事件:

private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if ((e.Item is GridDataItem)) {
        GridDataItem item = e.Item;
        DropDownList list = (DropDownList)item.FindControl("ddlProgramme");
        list.SelectedValue = DataBinder.Eval(item.DataItem, "<Datafield_name>").ToString();
    }
}

希望这有助于......