分享

GRIDVIEW后台绑定数据源问题

 复杂网络621 2013-09-28
GRIDVIEW后台绑定数据源:
string sql = "select * from DieReason_tb";
DataTable dt= Com2000888.Common.DbHelperSQL.GetDataTable(sql);
//GridView1.DataSource = dt;
//GridView1.DataBound();
DataView dv = dt.DefaultView;
UI.BindCtrl(dv.Count, dv, this.GridView1, this.AspNetPager2);
1.GRIDVIEW实现删除功能,需要在GRIDVIEW中加一删除列,并且要在方法 GridView1_RowDeleting中自已写代码实现删除功能!
2.GRIDVIEW实现编辑功能:首先需要在GRIDVIEW中加一选择列,并在GRIDVIEW的编辑模式中加上两个LINKBUTTON,分别是更新和取消,加入此方法:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.EditIndex = e.NewSelectedIndex;
BindDeathReason();
}
还要分别写更新方法,取消方法:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex][0].ToString();
DieReasonEntity dr = DieReasonEntityAction.RetrieveADieReasonEntity(int.Parse(id));
TextBox txtDeathReason = GridView1.Rows[e.RowIndex].FindControl("txtDeathReason") as TextBox;
dr.REASON = txtDeathReason.Text.ToString();
dr.Save();
GridView1.EditIndex = -1;
BindDeathReason();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindDeathReason();
}
3.更新字段的值老是不成功:后来才发现是因为:

protected void Page_Load(object sender, EventArgs e)
{
BindDeathReason();
},每次刷新后,都执行绑定,新的内容被替换为原来的内容。解决办法是加上:

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)

{
BindDeathReason();

}
}

这样就只会在第一次加载页时才绑定GRIDVIEW.

4.找控件失败:要寻找GRIDVIEW的编辑模式下的一个文本框,不用直接用GRIDVIEW.FINDCONTROL,这样找不到,如果用以下方式就可以找到:

TextBox txtDeathReason = GridView1.Rows[e.RowIndex].FindControl("txtDeathReason") as TextBox;

明确到哪一行找控件!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多