我正在尝试从后面的代码绑定gridview中的下拉列表。最初,我为gridview生成一个空行,我想在生成空行时绑定下拉列表。请帮忙.. aspx代码
<form id="form1" runat="server">
<asp:DropDownList ID="ddlSelectWeek" runat="server" OnSelectedIndexChanged="ddlSelectWeek_SelectedIndexChanged" Width="200px" AutoPostBack="true">
<asp:ListItem Text="2015-11-15" Value="2015-11-15"></asp:ListItem>
<asp:ListItem Text="2015-11-22" Value="2015-11-22"></asp:ListItem>
<asp:ListItem Text="2015-11-29" Value="2015-11-29"></asp:ListItem>
</asp:DropDownList>
<div>
<asp:GridView ID="grvStudentDetails" runat="server"
ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row ID" />
<asp:TemplateField HeaderText="Task Name">
<ItemTemplate>
<asp:DropDownList ID="drpQualification" runat="server">
<asp:ListItem Value="G">test1</asp:ListItem>
<asp:ListItem Value="P">test2</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server"
Text="Add New Row" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Day1">
<ItemTemplate>
<asp:TextBox ID="txtDay1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Day2">
<ItemTemplate>
<asp:TextBox ID="txtDay2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
背后的代码
protected void ddlSelectWeek_SelectedIndexChanged(object sender, EventArgs e)
{
DateTime theDate;
DateTime.TryParseExact(this.ddlSelectWeek.SelectedValue, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out theDate);
FirstGridViewRow(theDate);
}
private void FirstGridViewRow(DateTime theDate)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
dr = dt.NewRow();
for (var i = 1; i < 6; i++)
{
dr["RowNumber"] = i;
dr["Col1"] = string.Empty;
dr["Col2"] = string.Empty;
dr["Col3"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
}
ViewState["CurrentTable"] = dt;
grvStudentDetails.DataSource = dt;
grvStudentDetails.DataBind();
for (int i = 2; i < 9; i++)
{
grvStudentDetails.HeaderRow.Cells[i].Text = theDate.AddDays(i - 2).ToString("ddd") + " " + theDate.AddDays(i - 2).ToShortDateString();
}
TextBox txn = (TextBox)grvStudentDetails.Rows[0].Cells[1].FindControl("txtDay1");
txn.Focus();
Button btnAdd = (Button)grvStudentDetails.FooterRow.Cells[2].FindControl("ButtonAdd");
Page.Form.DefaultFocus = btnAdd.ClientID;
}
答案 0 :(得分:1)
基本想法非常简单。订阅GridView的RowDataBound事件:
<asp:GridView ID="grvStudentDetails" runat="server"
...
OnRowDataBound="grvStudentDetails_RowDataBound"
在其中找到在此行中创建的下拉控件并绑定它:
protected void grvStudentDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("drpQualification");
ddl.DataSource = your_data_source_here;
ddl.DataBind();
}
}