两个gridview控件联动的问题 效果是这样的,现在我有两个gridview控件,gridview1和gridview2 ,在gridview1中存放的全校所有班级的列表,我现在想实现的效果是,当在gridview1中选中一行的时候,gridview2中就显示该班级的所有同学的信息 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用gridview1的CellContentClick事件或click事件,触发gridview2的datasource的查询结果集改变,然后刷新gridview2 gridview1 selectedIndex事件里先获取选中行的班级ID,根据该ID再去组织sql获取其学生信息再绑定gridview2当然也可以在其他事件里,用linkbutton来实现 比如RowCommand事件现在机子上没源代码,只能给你参考思路 <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <asp:GridView ID="gvMain" runat="server" OnSelectedIndexChanging="gvMain_SelectedIndexChanging"> <Columns> <asp:CommandField ButtonType="Link" SelectText="选择" ShowSelectButton="true" /> </Columns> </asp:GridView> <br /> <asp:GridView ID="gv" runat="server"> </asp:GridView> </form></body></html>protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind")) { SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT CategoryID, CategoryName,Description FROM Categories", connection); DataSet dataSet = new DataSet(); dataAdpater.Fill(dataSet, "Categories"); if (dataSet.Tables.Contains("Categories")) { gvMain.DataSource = dataSet.Tables["Categories"].DefaultView; gvMain.DataKeyNames = new string[] { "CategoryID" }; gvMain.DataBind(); } } } } protected void gvMain_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind")) { SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT * FROM Products where CategoryID=@CategoryID ", connection); dataAdpater.SelectCommand.Parameters.Add(new SqlParameter("@CategoryID", gvMain.DataKeys[e.NewSelectedIndex].Value)); DataSet dataSet = new DataSet(); dataAdpater.Fill(dataSet, "Products"); if (dataSet.Tables.Contains("Products")) { gv.DataSource = dataSet.Tables["Products"].DefaultView; gv.DataBind(); } } } ID可以用Gridview.DataKeys[Gridview.SelectIndex].Value来获取 这样的问题本很简单,但你在这儿问又觉得几行代码很难让你看懂,最好发到程序到你的邮箱.留个EMAIL发几个代码文件给你就可以了, WndProcHooker 请教控制台窗口的隐藏与显示? 和数据库连接是否应该放到线程里?? 怎样将图片存储到SQL数据库中 怎么样才算启动sqlserver2005?怎么样才能连接上数据库呢?急 怎么在winform datagrid的最前面添加一checkbox列? 求助?无法在System.int32和System.string上执行“like”操作 关于Random的Next()成员函数问题 DataView的Sort方法中排序字段的先后对排序结果有没有影响? DataSet并发错误?如何解决?求救! C#Winform程序经常自动关闭。 问个继承的问题
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvMain" runat="server" OnSelectedIndexChanging="gvMain_SelectedIndexChanging">
<Columns>
<asp:CommandField ButtonType="Link" SelectText="选择" ShowSelectButton="true" />
</Columns>
</asp:GridView>
<br />
<asp:GridView ID="gv" runat="server">
</asp:GridView>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind"))
{ SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT CategoryID, CategoryName,Description FROM Categories", connection); DataSet dataSet = new DataSet();
dataAdpater.Fill(dataSet, "Categories"); if (dataSet.Tables.Contains("Categories"))
{
gvMain.DataSource = dataSet.Tables["Categories"].DefaultView;
gvMain.DataKeyNames = new string[] { "CategoryID" };
gvMain.DataBind();
} }
}
}
protected void gvMain_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{ using (SqlConnection connection = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Northwind"))
{ SqlDataAdapter dataAdpater = new SqlDataAdapter("SELECT * FROM Products where CategoryID=@CategoryID ", connection); dataAdpater.SelectCommand.Parameters.Add(new SqlParameter("@CategoryID", gvMain.DataKeys[e.NewSelectedIndex].Value)); DataSet dataSet = new DataSet();
dataAdpater.Fill(dataSet, "Products"); if (dataSet.Tables.Contains("Products"))
{
gv.DataSource = dataSet.Tables["Products"].DefaultView;
gv.DataBind();
} }
}