我正在尝试将一个组合框添加到网格视图中的字段,但是,我不知道如何用Sql中的表中的数据填充它。我知道如何正常地做到这一点,但我已经在网上寻找如何做一个存储在网格视图中的答案。我一直在尝试很多方法,而这正是我设法提出的方法,但它无法正常工作。
String Sql = @" select Staffid, staffforename, staffsurname, CONCAT(staffforename, ' ', staffSurname) as FullName from Staff where SupportTeamID = 'TEL'";
SqlConnection conn = new SqlConnection(Properties.Resources.cString);
SqlDataAdapter DA = new SqlDataAdapter(Sql, Properties.Resources.cString);
DataSet DS = new DataSet();
DA.Fill(DS, "STaff");
DataTable DT = DS.Tables["Staff"];
gv_Quals.FindControl("cmbStaffID").DataValueField = "StaffID";
gv_Quals.FindControl("cmbStaffID").DataTextField = "FullName";
gv_Quals.FindControl("cmbStaffID").DataSource = DT;
gv_Quals.FindControl("cmbStaffID").DataBind();
以下是存储在网格视图中的下拉列表的标记代码
<asp:TemplateField HeaderText="staffID">
<ItemTemplate>
<asp:DropDownList ID="cmbStaffID" runat="server"></asp:DropDownList>
</ItemTemplate>
答案 0 :(得分:0)
您应该为不同的行添加绑定代码。因此,为此定义RowDataBound
方法。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String Sql = @" select Staffid, staffforename, staffsurname, CONCAT(staffforename, ' ', staffSurname) as FullName from Staff where SupportTeamID = 'TEL'";
SqlConnection conn = new SqlConnection(Properties.Resources.cString);
SqlDataAdapter DA = new SqlDataAdapter(Sql, Properties.Resources.cString);
DataSet DS = new DataSet();
DA.Fill(DS, "STaff");
DataTable DT = DS.Tables["Staff"];
gv_Quals.FindControl("cmbStaffID").DataValueField = "StaffID";
gv_Quals.FindControl("cmbStaffID").DataTextField = "FullName";
gv_Quals.FindControl("cmbStaffID").DataSource = DT;
gv_Quals.FindControl("cmbStaffID").DataBind();
}
}
答案 1 :(得分:0)
将您的代码添加到GridViewID_RowDataBound事件。