2013-01-14 28 views
5

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

到目前为止,我只创建了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"; 
+3

更好的表现出一定的代码。你有什么尝试? – Ofiris

+0

尝试在这里进行搜索[.NET示例吨](http://www.google.com) – MethodMan

+0

我编辑了帖子。这就是我到目前为止= \ 我已经在那里搜索,我仍然在做...谢谢 – Ghaleon

回答

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); 
} 
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> 
+0

我可以做一个普通的Query,它返回一个DataSet并对数据集进行foreach并填充下拉列表?用这种方法你给我看? – Ghaleon

+0

@Ghaleon为网格的每一行调用方法'grvGrid_RowBound'。所以你的数据集_dsource可以是一个DataTable或DataView,如果我理解你正确..你可以阅读http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound。 aspx for more info – ajp

+0

否@Ghaleon,您将不得不使用ddlList.Items.Add()添加这种情况下的项目。 – StingyJack

1

ddlList.DataTextField = "Text";如果这一行给你一个错误,那么确保在你的数据源或数据集中有相同的列名。 如果其文本名,那么你应该分配ddlList.DataTextField = "textname";

只是一个念头!

+0

我也检查过。以为你也一样!但它是好的,该方法返回两列,名为:'prioridade'和'id',但仍然错误= \\ – Ghaleon

+0

但现在的错误是在我设置数据源的行:ddlist.DataSource = ds; – Ghaleon

+0

你有什么值ds?使用断点并检查它实际返回的内容。你的aspx页面中有SQLDATASOURCE吗? – rach

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")); 
相关问题