如何在 DataGrid 中填充 DropDownList?
How to populate a DropDownList in a 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(称为 ddlTrailerLocation),因此用户可以 select 预告片的位置,然后 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 中添加了代码。下拉列表现在显示一个位置,但它不是正确的位置
{
我不确定你是否真的用谷歌搜索过。几周前我使用了 DataSet,我使用 MSDN 来解决这个问题。
sqlDataAdapter1.Fill(dataset1.Tables["Customers"]);
您通常应该提供要将数据加载到其中的 DataTable 的名称。如果您传入 DataSet 的名称而不是特定数据 table,则会将名为 Table1 的 DataTable 添加到数据集中并加载数据库中的结果(而不是将数据加载到数据集)。有关详细信息,请参阅从数据适配器填充数据集。
首先在data-grid中添加一个隐藏字段控件,用于保存行的位置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"));
}
}
我有一个显示预告片信息的 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),因此用户可以 select 预告片的位置,然后 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 中添加了代码。下拉列表现在显示一个位置,但它不是正确的位置 {
我不确定你是否真的用谷歌搜索过。几周前我使用了 DataSet,我使用 MSDN 来解决这个问题。
sqlDataAdapter1.Fill(dataset1.Tables["Customers"]);
您通常应该提供要将数据加载到其中的 DataTable 的名称。如果您传入 DataSet 的名称而不是特定数据 table,则会将名为 Table1 的 DataTable 添加到数据集中并加载数据库中的结果(而不是将数据加载到数据集)。有关详细信息,请参阅从数据适配器填充数据集。
首先在data-grid中添加一个隐藏字段控件,用于保存行的位置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"));
}
}