从我的数据库中,我在查询数据库之后正在重新检索数据表。 我的asp.net Web表单应用程序我有GridView,它有BoundField和DropDownList。
我想将LectureID绑定到BoundField,并将pariculat讲座的主题绑定到DropDownList。
不知道如何进行必要的叮咬。
LectureID SubjectName
1 Sub1
1 Sub2
1 Sub3
1 Sub4
2 Sub1
2 Sub4
答案 0 :(得分:0)
<asp:GridView ID="grdvDetail" runat="server">
<Columns>
<asp:BoundField HeaderText="LectureID" DataField="LectureID" SortExpression="LectureID">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle Font-Size="11px" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlSubject" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
//Fill DataTable Using SQL Query
grdvDetail.DataSource = dt;
grdvDetail.DataBind();
for (int i = 0; i < grdvDetail.Rows.Count; i++)
{
DropDownList ddlSubject = ((DropDownList)grvProduct.Rows[i].FindControl("ddlSubject"));
DataTable dtDDL = new DataTable();
//Fill DataTable Using SQL Query
ddlSubject.DataSource = dtDDL;
ddlSubject.DataTextField = "SubjectName";
ddlSubject.DataValueField = "SubjectName";
ddlSubject.DataBind();
}
}
}
答案 1 :(得分:0)
可能不是完美的解决方案。想法是从给定的数据表创建一个Key,Value集合。
Dictionary
集合用于将作为Key
的讲座ID和List<string>
中的相关主题保持为Value
。
GridView为bind with Dictionary
,在GridView的RowDataBound event
中,每个dropdownlist
都会根据需要填充。
<强>标记强>:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Key" HeaderText="Lecture ID" />
<asp:TemplateField HeaderText="Subject Name">
<ItemTemplate>
<asp:DropDownList ID="ddlSubject" runat="server" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//test data
DataTable dt = new DataTable();
dt.Columns.Add("LectureID", typeof(int));
dt.Columns.Add("SubjectName");
dt.Rows.Add(1, "Sub1");
dt.Rows.Add(1, "Sub2");
dt.Rows.Add(1, "Sub3");
dt.Rows.Add(1, "Sub4");
dt.Rows.Add(2, "Sub1");
dt.Rows.Add(2, "Sub4");
dt.AcceptChanges();
//Bind with GridView with Dictionary collection
GridView1.DataSource = GetDictionary(dt);
GridView1.DataBind();
}
}
//get a dictionary of distinct lectureID
private Dictionary<int, List<string>> GetDictionary(DataTable dt)
{
var dictionary = new Dictionary<int, List<string>>();
foreach (DataRow dr in dt.Rows)
{
int iKey = Convert.ToInt32(dr["LectureID"]);
if (dictionary.ContainsKey(iKey))
{
List<string> lst = dictionary[iKey] as List<string>;
lst.Add(dr["SubjectName"].ToString());
dictionary[iKey] = lst;
}
else
{
var lst = new List<string>();
lst.Add(dr["SubjectName"].ToString());
dictionary.Add(iKey, lst);
}
}
return dictionary;
}
行数据绑定事件:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.DataItem != null)
{
DropDownList ddl = e.Row.FindControl("ddlSubject") as DropDownList;
ddl.DataSource = ((KeyValuePair<int, List<string>>)e.Row.DataItem).Value;
ddl.DataBind();
}
}
结果与DropDownList中各个主题的LectureID不同: