请问怎样对GridView模板列EditItem中的DropDownList进行数据绑定
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sCgyNam = ((Label)e.Row.Cells[1].FindControl( "lbCgyNam ")).Text.Trim();
DropDownList drop = (DropDownList)e.Row.FindControl( "dropCgyNam "); // 找不到dropCgyNam
Function.BindDropDownList(drop, "nam ", "id ", "类别 ");
//drop.Items.FindByText(sCgyNam).Selected = true;
}
}
------解决方案--------------------把你的.aspx中这个模版列的定义也贴出来,另外:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sCgyNam = ((Label)e.Row.Cells[1].FindControl( "lbCgyNam ")).Text.Trim();
DropDownList drop = (DropDownList)e.Row.FindControl( "dropCgyNam "); // 找不到dropCgyNam
if(drop!=null)
{
Function.BindDropDownList(drop, "nam ", "id ", "类别 ");
}
}
}
------解决方案--------------------up
------解决方案--------------------protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//保存当前行的au_id的值
string au_id = this.GridView1.DataKeys[e.Row.RowIndex][ "au_id "].ToString();
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl( "dropTemp ");
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
string strSQL = "select au_id from authors ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
dropTemp.DataSource = cmd.ExecuteReader();
dropTemp.DataTextField = "au_id ";
dropTemp.DataBind();
//到DropDownList中根据au_id的值去找需要设置为选中状态的项目,将其设置为选中
ListItem item = dropTemp.Items.FindByText(au_id);
if(item != null)
{
item.Selected = true;
}
cn.Close();
}
}
------解决方案--------------------这几天被这个困扰了,绑定