RowDataBound是数据绑定的时候才会触发,RowCreated是每次呈现的时候都要触发,RowCreated先于RowDataBound执行。
举个列,如果页面回发后发生过GridView.databind(),那么两个事件都会触发。如果没发生GridView.databind(),那么就只会触发RowCreated。
事件执行顺序:
一、GridView 显示绑定的数据顺序如下:
DataBinding ->RowCreated ->RowDataBound ->...... ->DataBound
/// <summary>
/// 初始化gridview时绑定编辑里的下拉框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvCustomerList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
{
string id = this.gdvCustomerList.DataKeys[e.Row.RowIndex].Value.ToString();
//取得所属行业列表
DataTable dtTrade = customerDetailBLL.GetTradeListByTypeName();
//自定义下拉列表
NewDropDownList ddl = new NewDropDownList(dtTrade);
ddl.ID = "ddlss";
ddl.DataTextField = "kindname";
ddl.DataValueField = "kindid";
ddl.DataSource = dtTrade;
ddl.DataBind();
ddl.SelectedValue = customerBLL.Trade(id).Rows[0][0].ToString();
e.Row.Cells[10].Controls.Add(ddl);
}
}
/// <summary>
/// 初始化gridview时绑定编辑里的下拉框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvCustomerList_RowCreated(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
//和下句的效果是一样的,编辑时偶数行,奇数行都可以添加到
if ((e.Row.RowState & DataControlRowState.Edit) != 0)
{
string id = this.gdvCustomerList.DataKeys[e.Row.RowIndex].Value.ToString();
//取得所属行业列表
DataTable dtTrade = customerDetailBLL.GetTradeListByTypeName();
//自定义下拉列表
NewDropDownList ddl = new NewDropDownList(dtTrade);
ddl.ID = "ddlss";
ddl.DataTextField = "kindname";
ddl.DataValueField = "kindid";
ddl.DataSource = dtTrade;
ddl.DataBind();
ddl.SelectedValue = customerBLL.Trade(id).Rows[0][0].ToString();
e.Row.Cells[10].Controls.Add(ddl);
}
}