C# 在GridView中绑定DropDownList的数据
我知道这个话题已经讨论了很多,但我找不到解决问题的办法 我曾尝试使用SqlDataSource创建一个字段绑定,但我遵循了每一步,最后字段绑定选项变灰 所以我开始尝试将DropDownList编码到GridView中,但是我目前没有在DDL中生成任何数据。不确定下一步要添加什么 下面是我的GridView代码,我试图让DDLContegory显示我的category表中的所有类别C# 在GridView中绑定DropDownList的数据,c#,asp.net,gridview,C#,Asp.net,Gridview,我知道这个话题已经讨论了很多,但我找不到解决问题的办法 我曾尝试使用SqlDataSource创建一个字段绑定,但我遵循了每一步,最后字段绑定选项变灰 所以我开始尝试将DropDownList编码到GridView中,但是我目前没有在DDL中生成任何数据。不确定下一步要添加什么 下面是我的GridView代码,我试图让DDLContegory显示我的category表中的所有类别 <asp:GridView ID="GridView1" runat="server" AutoGenerat
<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>
提前谢谢 我也遇到过类似的问题,您需要使用类似的东西private void BindDropDownList()
我的建议如下: 创建从数据库返回数据集的方法,如下所示:
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绑定到数据源,如:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers");
GridView1.DataBind();
}
}
gridview的on The GridView1_RowDataBound事件执行以下操作:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row.
DropDownList ddlCategory= (e.Row.FindControl("ddlCategory") as DropDownList);
ddlCategory.DataSource = GetData("SELECT DISTINCT Country FROM Category");
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryId";
ddlCategory.DataBind();
}
}
您可以使用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();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row.
DropDownList ddlCategory= (e.Row.FindControl("ddlCategory") as DropDownList);
ddlCategory.DataSource = GetData("SELECT DISTINCT Country FROM Category");
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryId";
ddlCategory.DataBind();
}
}
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();
}
}