GridView 绑定问题 在线等!

weilinyao 2007-12-07 03:49:35
我用2个GridView控件 分别绑定2个数据源 一个是sqldatasource 一个是objectdatasource 分别绑定
绑定以后 在2个GridView 分别加了2个按钮 一个是编辑一个是取消 (当然commandName 是edit,cancle )
现在出现的问题就是 和sqldatasource 数据源绑定的Grid 在点击编辑和取消时候没有问题
但是和objectdatasource 数据源绑定的Grid 需要点击2次才能触发编辑或者取消事件。
后台代码也不太相同
1绑定sqldatasource :
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
}
只需要写取消的代码,编辑的代码不用写;点击操作只需要1次。(正常)
2绑定objectdatasource:
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView2.EditIndex = -1;
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView2.EditIndex = e.NewEditIndex;
}
编辑和取消的代码都要写,而且效果是要操作2次才可以。(不正常)
...全文
82 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weilinyao 2007-12-07
  • 打赏
  • 举报
回复
说的没错 ,是我犯错了,呵呵谢谢
心情解码 2007-12-07
  • 打赏
  • 举报
回复
每执行操作完成


要重新绑定数据。
cpp2017 2007-12-07
  • 打赏
  • 举报
回复
用this.GridView1.DataBind()是指用普通的object srouce方式

如果你自己指定DataSource 那么就要重新取一次数据.即像你那样调用getLeaves方法.
weilinyao 2007-12-07
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
getLeaves();
}
}
protected void getLeaves()
{

DataTable dt = new DataTable();
dt = LeavaWordsBll.GetAllLeaves("1");
this.GridView2.DataSource = dt.DefaultView;
this.GridView2.DataBind();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView2.EditIndex = -1;
getLeaves();
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView2.EditIndex = e.NewEditIndex;
getLeaves();
}

这样就可以了。我一开始也用的是楼上的方法,点击以后就没有数据了,
不能用this.GridView1.DataBind();
而是要整个的绑定一次,不过也要谢谢楼上的。
cpp2017 2007-12-07
  • 打赏
  • 举报
回复
重新绑定一下.写完编辑或取消后.this.GridView1.EditIndex = -1;
this.GridView1.DataBind();

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧