如何将DataSource设置为DropDownList?

时间:2013-01-14 17:18:13

标签: c# asp.net visual-studio-2010 web

我读了一些其他的线程并没有为我工作= \ 我在一个字段中有GridViewDropDownList。我想知道如何为此设置DataSource?我没有使用模板既不是ItemTemplate也不是EditItemTemplate我不知道它是如何工作的,所以我还没有使用它。

到目前为止,我只创建了GridView并用数据填充了字段,但我不知道如何对DropDownList执行相同操作。我猜错了,它给了我一个错误("The Reference of the Object was not set as an instance of an object"

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {                                
            DropDownList Drop_Prioridades = (DropDownList)e.Row.FindControl("Drop_Prioridades");
            Drop_Prioridades.DataTextField = "BAIXA";
            Drop_Prioridades.DataValueField = "1";
            Drop_Prioridades.DataTextField = "MEDIA";
            Drop_Prioridades.DataValueField = "2";
            Drop_Prioridades.DataTextField = "ALTA";
            Drop_Prioridades.DataValueField = "3";
            Drop_Prioridades.DataBind();
        }

我也试过这个/同样的错误= \

DataSet ds = func.LoadPriority();

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ListItem item = new ListItem();
                item.Text = row["prioridade"].ToString();
                item.Value = row["id"].ToString();
                DropDownList ddlPrioridades = (DropDownList)e.Row.FindControl("Drop_Prioridades");
                ddlPrioridades.Items.Add(item);
            }

并试过这个......

HTML:

<columns>                    

     <asp:TemplateField HeaderText="PRIORIDADE" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100px">
            <ItemTemplate>
                <asp:DropDownList ID="Drop_Prioridades" Width="120px" runat="server" ></asp:DropDownList> 
</ItemTemplate>                    
</asp:TemplateField>

代码背后:

 DataSet ds = func.CarregaPrioridade();
            DropDownList ddlist = (DropDownList)e.Row.FindControl("Drop_Prioridades");
            ddlist.DataSource = ds;
            ddlist.DataTextField = "prioridade";
            ddlist.DataValueField = "id";

4 个答案:

答案 0 :(得分:1)

在标记中绑定行数据绑定事件,如下所示:

<asp:GridView ID="grvGrid" runat="server" OnRowDataBound="grvGrid_RowBound">
   <Columns>
       <asp:TemplateField ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"  ItemStyle-Width="7%">
                <ItemTemplate>
                   <asp:DropDownList ID="ddlList" runat="server"/>
                </ItemTemplate>
       </asp:TemplateField>
    </Columns>
 </asp:GridView?

在背后的代码中:

 protected void grvGrid_RowBound(object sender, GridViewRowEventArgs e)
 {
     DropDownList ddlList= (DropDownList )e.Row.FindControl("ddlList");
     ddlList.DataSource = _dSource;
     ddlList.DataTextField = "text";
     ddlList.DataValueField = "value";
     ddlList.DataBind();


  }

OR

如果你的下拉列表每行都有相同的选项,你就不需要在RowDataBound事件中绑定它。 您可以将项目添加到标记中的下拉列表中,如下所示:

 <asp:DropDownList id="ddlList"runat="server">

              <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
              <asp:ListItem Value="Silver"> Silver </asp:ListItem>
              <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
              <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
              <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

           </asp:DropDownList>

答案 1 :(得分:1)

ddlList.DataTextField = "Text";如果此行显示错误,请确保在数据源或数据集中具有相同的列名称。 如果是文本名称,则应指定ddlList.DataTextField = "textname";

只是一个想法!

答案 2 :(得分:1)

请遵循此代码将数据源分配给下拉列表

    DataTable dt = ds.tables[0];
    DropdownId.DataSource = dt;
    DropdownId.DataTextField = "Text Column";
    DropdownId.DataValueField = "Value Column";
    DropdownId.DataBind();
    DropdownId.Items.Insert(0, new ListItem("-- Select --", "0"));

答案 3 :(得分:0)

我解决了这个问题:

DataSet ds = SomeMethodToFillTheDataSet()

foreach(DataRow row in ds.tables[0].Rows)
{
  ListItem item = new ListItem();
  item.text = "fieldName";  e.g  Name
  item.value = "FieldName"; e.g  ID
  DropDOwnList.Items.Add(item);
}