【问题标题】:How to populate a DropDownList in a DataGrid?如何在 DataGrid 中填充 DropDownList?
【发布时间】:2015-08-22 23:58:27
【问题描述】:

我有一个显示预告片信息的 DataGrid。我决定将位置列更改为 DropDownList,以便可以轻松更改位置。但我不确定如何填充 DropDownList。

 <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" OnRowDataBound="OnRowDataBound" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
                <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                <asp:TemplateColumn HeaderText="Trailer Location">
                    <itemtemplate>
                        <asp:DropDownList ID="ddlLocation" runat="server">
                        </asp:DropDownList>
                 </itemtemplate>
                </asp:TemplateColumn>
                </Columns>
        </asp:DataGrid>

我已经有一个用于位置的 DropDownList(称为 ddlTrailerLocation),因此用户可以选择预告片的位置,然后 DataGrid 会显示所有这些信息。

protected void PopulateDDLs()
{
    DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
    if (dsTrailerLocation.Tables[0].Rows.Count > 0)
    {
        ddlTrailerLocation.DataSource = dsTrailerLocation;
        ddlTrailerLocation.DataValueField = "Description";
        ddlTrailerLocation.DataTextField = "Description";
        ddlTrailerLocation.DataBind();
        ddlTrailerLocation.Items.Insert(0, new ListItem("Select One", "0"));
    }
    else
    {
        ddlTrailerLocation.Items.Insert(0, new ListItem("No Locations Entered", "0"));
    }
}

protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
    {
    DropDownList ddlTrailerLocation = e.Item.FindControl("ddlLocation") as DropDownList;
         DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
         if (ddlTrailerLocation != null)
         {
             ddlTrailerLocation.DataSource = dsTrailerLocation;
             ddlTrailerLocation.DataValueField = "Description";
             ddlTrailerLocation.DataTextField = "Description";
             ddlTrailerLocation.DataBind();
         }
}

编辑 在受保护的 void dgList_ItemCreated 中添加了代码。下拉列表现在显示一个位置,但它不是正确的位置 {

【问题讨论】:

  • 你先用谷歌搜索了吗?
  • 是的,但我不知道该怎么做,因为下拉列表已经存在,所以我不得不为数据网格中的新下拉列表提供不同的 ID。我不确定我需要什么 c# 代码来填充它
  • 您可以在网格视图中提供任何名称 id,并在行数据绑定事件中使用不同的别名进行访问。这是示例aspsnippets.com/Articles/…

标签: c# html asp.net drop-down-menu datagrid


【解决方案1】:

首先在数据网格中添加一个隐藏字段控件以保存行的位置 ID。之后,当绑定下拉菜单时,您需要从隐藏字段中获取值并在下拉菜单中进行设置,如下所示。

 <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" OnRowDataBound="OnRowDataBound" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
                <HeaderStyle CssClass="tblResultsHeader" />
                <AlternatingItemStyle BackColor="#EEEEEE" />
                <Columns>
                    <asp:HyperLinkColumn ItemStyle-CssClass="loading" DataNavigateUrlFormatString="Trailer.aspx?TrailerID={0}" DataNavigateUrlField="ID" DataTextField="Reg" HeaderText="Registration" SortExpression="Reg"></asp:HyperLinkColumn>
                    <asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
                    <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                    <asp:TemplateColumn HeaderText="Trailer Location">
                        <itemtemplate>
                            <asp:DropDownList ID="ddlTrailerLoc" runat="server">
                            </asp:DropDownList>
                            <asp:HiddenField ID="hdlTrailerLoc" runat="server" Value='<%#Eval("LocationId")%>' />
                     </itemtemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>
                    <asp:BoundColumn DataField="TrailerNumber" HeaderText="Trailer Number" SortExpression="TrailerNumber"></asp:BoundColumn>
                    <asp:BoundColumn DataField="DateOfLastService" HeaderText="Last Service" SortExpression="DateOfLastService"></asp:BoundColumn>
                    <asp:BoundColumn DataField="DateOfNextService" HeaderText="Next Service" SortExpression="DateOfNextService"></asp:BoundColumn>
                    <asp:BoundColumn DataField="IsActive" HeaderText="Is Active" SortExpression="IsActive"></asp:BoundColumn>
                    </Columns>
            </asp:DataGrid>

请绑定下拉菜单并设置数据网格“ItemDataBound”事件的值如下

 protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager && e.Item.ItemType != ListItemType.Footer)
        {
            int count = 1;
            foreach (TableCell c in e.Item.Cells)
            {
                bool b = Convert.ToBoolean(((DataRowView)e.Item.DataItem).Row["IsActive"]);

                if (count == e.Item.Cells.Count)
                {
                    c.Text = "<input DISABLED type=\"checkbox\" " + ((b) ? "checked" : "") + "/>";
                }
                DateTime dt = new DateTime();
                if (DateTime.TryParse(c.Text, out dt))
                {
                    c.Text = dt.ToShortDateString();
                }
                count++;
            }

             DropDownList ddlTrailerLocation = e.Item.FindControl("ddlTrailerLoc") as DropDownList;
             //DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
             if (ddlTrailerLocation != null)
             {
                 PopulateDDLs(ddlTrailerLocation);
                 //set the value in dropdown
                 HiddenField hdlTrailerLoc = e.Item.FindControl("hdlTrailerLoc") as HiddenField;
                 if (hdlTrailerLoc != null)
                 {
                     ddlTrailerLocation.SelectedValue = hdlTrailerLoc.Value;
                 }
             }
        }
    }

下面的绑定位置下拉代码

protected void PopulateDDLs(DropDownList ddlTrailerLoc)
{
    DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
    if (dsTrailerLocation.Tables[0].Rows.Count > 0)
    {
        ddlTrailerLoc.DataSource = dsTrailerLocation;
        ddlTrailerLoc.DataValueField = "Description";
        ddlTrailerLoc.DataTextField = "Description";
        ddlTrailerLoc.DataBind();
        ddlTrailerLoc.Items.Insert(0, new ListItem("Select One", "0"));
    }
    else
    {
        ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
    }
}

【讨论】:

  • 我不知道如何处理绑定代码。和 PopulateDDL 的代码一样吗?
  • 请用一个参数 PopulateDDLs(DropDownList ddlTrailerLocation) 编写 PopulateDDLs 方法。让我更新代码
  • @DeepakJoshi 我编辑了我的问题,请查看添加的代码。但它没有显示正确的位置
  • 我已经更新了我的答案。请验证并确认。它是否适合你
  • 每行只显示一个位置。每个拖车都有不同的位置
【解决方案2】:

我不确定你是否真的用谷歌搜索过。几周前我使用了 DataSet,我在这个问题上使用了MSDN

sqlDataAdapter1.Fill(dataset1.Tables["Customers"]);

您通常应该提供要加载数据的 DataTable 的名称。如果您传入 DataSet 的名称而不是特定的数据表,则会将名为 Table1 的 DataTable 添加到数据集并加载来自数据库的结果(而不是将数据加载到数据集中的现有 DataTable 中)。有关详细信息,请参阅从 DataAdapter 填充数据集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多