我知道这个话题已被覆盖很多,但我无法找到解决问题的方法。
我已尝试使用SqlDataSource使用以下指南创建字段绑定,但我按照每个步骤进行操作,最后字段绑定的选项显示为灰色。 https://msdn.microsoft.com/en-us/library/ms178294.aspx
所以我开始试图将DropDownList编码到GridView中,但我目前没有在DDL中生成任何数据。不知道下一步要添加什么。
下面是我的GridView代码,我正在尝试让ddlCategory显示我的类别表中的所有类别。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="FileID" DataSourceID="filenameTableDataSource"
EnableModelValidation="True" Style="text-align: center">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="FileID" HeaderText="FileID" InsertVisible="False" ReadOnly="True" SortExpression="FileID" />
<asp:BoundField DataField="Filename" HeaderText="Filename" SortExpression="Filename" />
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<ItemTemplate>
<asp:Label ID="Category" runat="server" Visible="false" />
<asp:DropDownList ID="ddlCategory" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />--%>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:CheckBoxField DataField="IsPublished" HeaderText="IsPublished" SortExpression="IsPublished" />
<asp:CheckBoxField DataField="IsArchived" HeaderText="IsArchived" SortExpression="IsArchived" />
</Columns>
</asp:GridView>
这是我用来获取GridView数据的SQL语句:
SELECT Files.FileID, Files.CategoryID, Files.Filename, Files.Description,
Files.IsPublished, Files.IsArchived, Categories.Description AS Category
FROM Files INNER JOIN Categories ON Files.CategoryID = Categories.CategoryID
提前致谢!
答案 0 :(得分:0)
我有类似的斗争,你需要沿着这些行使用某些东西private void BindDropDownList()
DataView dv = mgr.GetItemSeriesMaster().DefaultView; //how to filter data
dv.RowFilter = ProductQueryFilter;
Dropdownlist1.DataSource = dv;
Dropdownlist1.DataTextField = "Description"; // the items to be displayed in the list items
Dropdownlist1.DataValueField = "Id"; // the id of the items displayed
Dropdownlist1.DataBind();
答案 1 :(得分:0)
我的建议如下: 创建一个从DataBase返回DataSet的方法,如下所示:
<link rel="stylesheet" href="{% static 'labelcreator/css/overview_edit_master.css' %}">
<div class="container">
<div class="row">
<div class="col-lg-12">
<form class="form-horizontal" method="POST">
{% csrf_token %}
<div class="form-group" id="productGroup">
<label for="inlineFormCustomSelectPref">Choose Product...</label>
{{ form.as_p }}
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary my-1 pull-right">Create
Label</button>
</div>
</form>
</div>
</div>
</div>
将GridView绑定到您的数据SOURce,如:
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
gridview的GridView1_RowDataBound事件如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers");
GridView1.DataBind();
}
}
答案 2 :(得分:0)
你可以使用OnRowDataBound事件:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCategory= (e.Row.FindControl("ddlCategory") as DropDownList);
ddlCategory.DataSource = GetData("SELECT DISTINCT CategoryName,CategoryId FROM Categories");
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryId";
ddlCategory.DataBind();
}
}