两个GridView联动

foxal 2011-02-23 10:29:48
首先,本人自学小白,请大家多多赐教
下面,本人在前台通过数据源绑定了两个GridView
GridView1:
A | B
1 | 2
1 | 6
3 | 3

GridView2:
A | B
1 | 3
2 | 5
2 | 3

现在点击GridView1中的一行,如果GridView1中的B和Girdview2中的A相同,如,点击1 | 2这行,则GridView2展示如下
GridView2:
A | B
2 | 5
2 | 3

现在本人做法是这样,现在load中绑定两个GridView,然后在SelectedIndexChanged事件中:
protected void gvTableA_SelectedIndexChanged(object sender, EventArgs e)
{
int dataA = int.Parse(gvTableA.SelectedRow.Cells[1].Text);

int titleB = int.Parse(gvTableB.SelectedRow.Cells[0].Text);

if (dataA != titleB)
{
this.gvTableB.DataSource = DataTableBManager.GetAll();
this.gvTableB.DataBind();
}
else
{
this.gvTableB.DataSource = DataTableBManager.GetAllByTitle(2);
this.gvTableB.DataBind();
}
}

但是达不到效果,望各位高手赐教
...全文
231 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
foxal 2011-02-24
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 li_1042237864 的回复:]
引用 17 楼 foxal 的回复:

引用 16 楼 li_1042237864 的回复:
C# code

//改成这样
protected void gvTableA_SelectedIndexChanged(object sender, EventArgs e)
{
int dataA = int.Parse(gvTableA.SelectedRow.Cells[1].Te……
[/Quote]

加了一个按钮解决了,至于单击单行还在研究中,万分感谢
foxal 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 li_1042237864 的回复:]
C# code

//改成这样
protected void gvTableA_SelectedIndexChanged(object sender, EventArgs e)
{
int dataA = int.Parse(gvTableA.SelectedRow.Cells[1].Text);

//int titleB = int.Parse(gvTableB.S……
[/Quote]

还是没反应,现在点击一行后根本是不走这个事件不知道哪儿的事儿
纯唇Yu弄 2011-02-23
  • 打赏
  • 举报
回复
 
//改成这样
protected void gvTableA_SelectedIndexChanged(object sender, EventArgs e)
{
int dataA = int.Parse(gvTableA.SelectedRow.Cells[1].Text);

//int titleB = int.Parse(gvTableB.SelectedRow.Cells[0].Text);

if (dataA != null && dataA!="")
{
this.gvTableB.DataSource = DataTableBManager.GetAllByTitle(dataA);
this.gvTableB.DataBind();
}
else
{
this.gvTableB.DataSource = DataTableBManager.GetAll();
this.gvTableB.DataBind();
}
}
foxal 2011-02-23
  • 打赏
  • 举报
回复
设置断电后发现根本不走那个事件......
foxal 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wxr0323 的回复:]
另外看你的事件执行没 打个断点 查看里面的值
[/Quote]
发现这个问题了,事件没有触发,正在找原因
foxal 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxr0323 的回复:]
C# code
if (dataA == titleB)
{

this.gvTableB.DataSource = DataTableBManager.GetAllByTitle(2);
this.gvTableB.DataBind();
}
else
{
this.gvTableB.DataSource = DataTableBManager……
[/Quote]

没有变化......
foxal 2011-02-23
  • 打赏
  • 举报
回复
补充一下, 我现在发现SelectedIndexChanged没有触发,我把id值绑定为2,随便点一行后应该都是2的那行数据被绑定,但是现在没有反应,不知道什么原因
子夜__ 2011-02-23
  • 打赏
  • 举报
回复
另外看你的事件执行没 打个断点 查看里面的值
子夜__ 2011-02-23
  • 打赏
  • 举报
回复
if (dataA == titleB)
{

this.gvTableB.DataSource = DataTableBManager.GetAllByTitle(2);
this.gvTableB.DataBind();
}
else
{
this.gvTableB.DataSource = DataTableBManager.GetAll();
this.gvTableB.DataBind();
}
}


改成这样。
foxal 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
gvTableA_SelectedIndexChanged
中通过dataset dt.Select查询gridview2中是否存在相同值,再查询绑定
[/Quote]
dataset用的很少,一时想不起来,本人刚刚开始学习,请多给些提示,万分感谢
wuyq11 2011-02-23
  • 打赏
  • 举报
回复
gvTableA_SelectedIndexChanged
中通过dataset dt.Select查询gridview2中是否存在相同值,再查询绑定
foxal 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nandy249407850 的回复:]
this.gvTableB.DataBind();
再 这一行后面加一句
Response.Redirect("当前页面的地址");
[/Quote]
都在一个页上
foxal 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 timzhufaith 的回复:]
主要问题在于如何获得点击的B值。。。看模板列
[/Quote]
<Columns>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="data" HeaderText="data" SortExpression="data" />
</Columns>
我用的是默认的,没有自定义模版,本人小白,请详解
nandy249407850 2011-02-23
  • 打赏
  • 举报
回复
this.gvTableB.DataBind();
再 这一行后面加一句
Response.Redirect("当前页面的地址");
TimZhuFaith 2011-02-23
  • 打赏
  • 举报
回复
主要问题在于如何获得点击的B值。。。看模板列
纯唇Yu弄 2011-02-23
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 foxal 的回复:]

引用 16 楼 li_1042237864 的回复:
C# code

//改成这样
protected void gvTableA_SelectedIndexChanged(object sender, EventArgs e)
{
int dataA = int.Parse(gvTableA.SelectedRow.Cells[1].Text);

//int titleB ……
[/Quote]

要把gridview的属性AutoGenerateSelectButton="True" 前边会出现一个选择按钮 点击触发事件

62,046

社区成员

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

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

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

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