打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
GRIDVIEW后台绑定数据源问题
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;

明确到哪一行找控件!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
GridView删除更新操作
asp.net使用GridView删除更新ACCESS数据库里的数据问题!急!!!
GridView和下拉菜单DropDownList结合
GridView更新代码
对GradView的增删改
gridview中如何定位?选中行的关键值
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服