如何在DataGrid中填充DropDownList?

时间:2015-06-08 13:31:09

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

我有一个显示有关预告片信息的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 for location(称为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();
         }
}

修改 在protected void dgList_ItemCreated中添加了代码。下拉列表现在显示一个位置,但它不是正确的位置     {

2 个答案:

答案 0 :(得分:1)

我不确定你是否真的用Google搜索过。几个星期前我使用了DataSet,我使用了MSDN来解决这个问题。

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

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

答案 1 :(得分: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>

请绑定下拉列表并在数据网格上设置值&#34; ItemDataBound&#34;事件如下

 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"));
    }
}