dropdownlist选择表后gridview显示出所选,要怎么联动呢?小弟初学C#,现在设置好dropdownlist,根据表名在SQL的系统表sysobjects选择表,AutoPostBack已经开了,但是不知道怎么样才能让gridview实现和dropdownlist的联动,就是在dropdownlist选择表然后gridview随之刷新GridView1+SqlDataSource1现在绑定了其中一个表
DropDownList1+SqlDataSource2是绑定系统表sysobjects看到网上有人说这个方法
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "select * from '" + this.DropDownList1.SelectedValue + "'";
}
但是不太明白
特此求教~!
DropDownList1+SqlDataSource2是绑定系统表sysobjects看到网上有人说这个方法
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "select * from '" + this.DropDownList1.SelectedValue + "'";
}
但是不太明白
特此求教~!
{
SqlDataSource1.SelectCommand = "select * from '" + this.DropDownList1.SelectedValue + "'";
}
==============================================================
this.DropDownList1.SelectedValue 指的是表的名字
是写在private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}中
-------------------
那应该是在DropDownList1_SelectedIndexChanged事件里,根据所选的项去数据库里查询,得到结果绑定到GridView1
{
SqlDataSource1.SelectCommand = "select * from '" + this.DropDownList1.SelectedValue + "'";
//下面要重新绑定数据
.............//重新绑定数据
}
多谢~
如果说要在GridView上面绑定,大概思路是怎样的呢?
重新连接数据库,然后用循环读取表里面所有的项的名字,再用SQL语句实现添加修改删除?
……
怎么觉得像自己写控件一样=。=
GridView数据绑定可以采用带变量的存储过程实现,变量类型选择ControlID,再选择DropDownList的SelectedValue.
Procedure Pr_ceshi
@DropDownlist_Parameter
as
select *
from sysobjects
where sysobjects.关联字段=@DropDownList_Parameter
GO
有不明白的再说!
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
找到一个能够借鉴的,顺手贴上来
=============================using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; public partial class Default2 :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindToDropDownList();//绑定DropDownList
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
this.BindToGridView(id); //绑定GridView
}
}
//绑定到 DropDownList
private void BindToDropDownList()
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn1"]);
SqlDataAdapter sda = new SqlDataAdapter("select ProductID,ProductName from products", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "ProductName";
this.DropDownList1.DataValueField = "ProductID";
this.DropDownList1.DataBind();
}
//绑定到GridView
private void BindToGridView(string id)
{
this.GridView1.AllowPaging=true;//允许分页
this.GridView1.AllowSorting = true;//允许排序
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn1"]);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from [Order Details]where ProductID=" + id, conn);
DataSet ds = new DataSet();
sda1.Fill(ds);
//如果不排序用下面的绑定方法就可以了
//this.GridView1.DataSource = ds;
//this.GridView1.DataBind();
//如果要排序采用下面的梆定方法
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
//回调数据库
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
this.BindToGridView(id);
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
this.GridView1.PageIndex = e.NewPageIndex;
this.BindToGridView(id);
}
//行变色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");//结合前台html代码
e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);");
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string id = this.DropDownList1.SelectedValue;//获取当前DropDownList的value值
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = "asc";
}
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
this.BindToGridView(id);//重新绑定
}
} //前台Javascript: 数据库:SQl2000 Northwind
create proc SelectTable
@tablename varchar(20)
as
declare @sql nvarchar(4000)
set @sql = 'select * from ' + @tablename
exec sp_executesql @sql
goaspx页面文件如下
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource2">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLexpress;Initial Catalog=pubs;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="select name from sysobjects where type='U'">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.\SQLexpress;Initial Catalog=pubs;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SelectTable" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="tablename" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>