SelectedIndexChanged该怎么用? winform下,如何通过选择combobox中下拉列表的值来改变绑定了表的datagrid?其中combobox所选的值为datagrid中的某一字段 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把comboBox1.Text传入sql语句,在重新执行绑定datagrid的动作 我的问题是:datagrid绑定了一个表table,table的一个字段是tableid,combobox中的值对应tableid,当在combobox选择了一个值例如user1时,datagrid显示的是tableid字段=user1的所有行.当重新选择combobox时,要调SelectedIndexChanged方法. 秋枫的代码,,,是好用的。我改了一点,你自己改一下吧。//using System;//using System.Collections;//using System.ComponentModel;//using System.Data;//using System.Data.SqlClient;//using System.Drawing;//using System.Web;//using System.Web.SessionState;//using System.Web.UI;//using System.Web.UI.WebControls;//using System.Web.UI.HtmlControls; //namespace WebApplication1//{// /// <summary>// /// WebForm28 的摘要说明。// /// </summary>// public class WebForm4 : System.Web.UI.Page// {// protected System.Web.UI.WebControls.DropDownList DropDownList1;// protected System.Web.UI.WebControls.DataGrid DataGrid1;// // private void Page_Load(object sender, System.EventArgs e)// {// // 在此处放置用户代码以初始化页面// if(!IsPostBack)// {// SetBind();//// SetBind2();// }// }//// protected void SetBind()// {//// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);// SqlDataAdapter da=new SqlDataAdapter("select 事件名,存储过程名 from _List_Event",conn);// DataSet ds=new DataSet();// da.Fill(ds,"table1");// this.DataGrid1.DataSource=ds.Tables["table1"];// this.DataGrid1.DataBind();// }////// protected void SetBind2()//// {//////// SqlConnection conn2=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);//// SqlDataAdapter da2=new SqlDataAdapter("select * from dep",conn2);//// DataSet ds2=new DataSet();//// da2.Fill(ds2,"table1");//// this.DropDownList1.DataSource=ds2.Tables["table1"];//// this.DropDownList1.DataTextField="depname";//// this.DropDownList1.DataValueField="depid";//// this.DropDownList1.DataBind();//// this.DropDownList1.Items.Insert(0,new ListItem("请选择",""));//// //// }//// protected void SetBind3()// {// string s=this.DropDownList1.SelectedValue;// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);// SqlCommand comm=new SqlCommand();// comm.Connection=conn;// if(s!="")// {// comm.CommandText="select * from stu,dep where stu.studepid=dep.depid and depid=@depid";// SqlParameter parm1=new SqlParameter("@depid",SqlDbType.Int);// parm1.Value=s;// comm.Parameters.Add(parm1);// }// else// comm.CommandText="select * from stu,dep where stu.studepid=dep.depid";// SqlDataAdapter da=new SqlDataAdapter();// da.SelectCommand=comm;// DataSet ds=new DataSet();// da.Fill(ds,"table1");// this.DataGrid1.DataSource=ds.Tables["table1"];// this.DataGrid1.DataBind();// // }// #region Web 窗体设计器生成的代码// override protected void OnInit(EventArgs e)// {// //// // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。// //// InitializeComponent();// base.OnInit(e);// }// // /// <summary>// /// 设计器支持所需的方法 - 不要使用代码编辑器修改// /// 此方法的内容。// /// </summary>// private void InitializeComponent()// { // this.Load += new System.EventHandler(this.Page_Load);//// }// #endregion//// private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)// {// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);// SqlDataAdapter da=new SqlDataAdapter("select * from dep",conn);// DataSet ds=new DataSet();// da.Fill(ds,"table1");// if(e.Item.ItemType==ListItemType.EditItem)// {// DropDownList ddl=(DropDownList)e.Item.FindControl("dep");// ddl.DataSource=ds.Tables["table1"];// ddl.DataTextField="depname";// ddl.DataValueField="depid";// ddl.DataBind();// ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;//选择数据库内的作为默认// }// }//// protected void edit(object sender,DataGridCommandEventArgs e)// {// this.DataGrid1.EditItemIndex=e.Item.ItemIndex;// if(this.DropDownList1.SelectedValue=="")// SetBind();// else// SetBind3();// }//// protected void cancel(object sender,DataGridCommandEventArgs e)// {// this.DataGrid1.EditItemIndex=-1;// if(this.DropDownList1.SelectedValue=="")// SetBind();// else// SetBind3();// }//// protected void update(object sender,DataGridCommandEventArgs e)// {// if(e.Item.ItemType==ListItemType.EditItem)//只有在编辑按下以后才能提交// {// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);// SqlCommand comm=new SqlCommand("update stu set stuname=@name,studepid=@depid where stuid=@id",conn);// SqlParameter parm1=new SqlParameter("@name",SqlDbType.NVarChar,50);// parm1.Value=((TextBox)e.Item.FindControl("name")).Text;// SqlParameter parm2=new SqlParameter("@depid",SqlDbType.Int);// parm2.Value=((DropDownList)e.Item.FindControl("dep")).SelectedValue;// SqlParameter parm3=new SqlParameter("@id",SqlDbType.Int);// parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];// comm.Parameters.Add(parm1);// comm.Parameters.Add(parm2);// comm.Parameters.Add(parm3);// conn.Open();// comm.ExecuteNonQuery();// conn.Close();// this.DataGrid1.EditItemIndex=-1;// if(this.DropDownList1.SelectedValue=="")// SetBind();// else// SetBind3();//如果选择过滤则使用SetBind3()// }// }//// private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)// {// SetBind3();// }// }//} 关于HttpWebRequest并发的纠结问题,请各位帮助 datagridview手动绑定列数据源 GroupBox不知何原因没边框线 如何在DATAGRID中的单元格集成时间格式 简单的问题,帮忙看看 储过程返回值的时候只返回值的第一个字母 郁闷中 关于TreeNode Clone()的一个问题 新手提问:如何显示另一窗体? 100分求救,我的vs203出问题了不能创建web程序了 C#读取XML文档的问题 关于本地化郁闷的问题?急!!!高手请进 我在WEB程序中使用listbox取值,为什么老是报错? ...在线
,当在combobox选择了一个值例如user1时,datagrid显示的是tableid字段=user1的所有行.当重新选择combobox时,要调SelectedIndexChanged方法.
//using System.Collections;
//using System.ComponentModel;
//using System.Data;
//using System.Data.SqlClient;
//using System.Drawing;
//using System.Web;
//using System.Web.SessionState;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.HtmlControls;
//namespace WebApplication1
//{
// /// <summary>
// /// WebForm28 的摘要说明。
// /// </summary>
// public class WebForm4 : System.Web.UI.Page
// {
// protected System.Web.UI.WebControls.DropDownList DropDownList1;
// protected System.Web.UI.WebControls.DataGrid DataGrid1;
//
// private void Page_Load(object sender, System.EventArgs e)
// {
// // 在此处放置用户代码以初始化页面
// if(!IsPostBack)
// {
// SetBind();
//// SetBind2();
// }
// }
//
// protected void SetBind()
// {
//
// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
// SqlDataAdapter da=new SqlDataAdapter("select 事件名,存储过程名 from _List_Event",conn);
// DataSet ds=new DataSet();
// da.Fill(ds,"table1");
// this.DataGrid1.DataSource=ds.Tables["table1"];
// this.DataGrid1.DataBind();
// }
//
//// protected void SetBind2()
//// {
////
//// SqlConnection conn2=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
//// SqlDataAdapter da2=new SqlDataAdapter("select * from dep",conn2);
//// DataSet ds2=new DataSet();
//// da2.Fill(ds2,"table1");
//// this.DropDownList1.DataSource=ds2.Tables["table1"];
//// this.DropDownList1.DataTextField="depname";
//// this.DropDownList1.DataValueField="depid";
//// this.DropDownList1.DataBind();
//// this.DropDownList1.Items.Insert(0,new ListItem("请选择",""));
////
//// }
//
// protected void SetBind3()
// {
// string s=this.DropDownList1.SelectedValue;
// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
// SqlCommand comm=new SqlCommand();
// comm.Connection=conn;
// if(s!="")
// {
// comm.CommandText="select * from stu,dep where stu.studepid=dep.depid and depid=@depid";
// SqlParameter parm1=new SqlParameter("@depid",SqlDbType.Int);
// parm1.Value=s;
// comm.Parameters.Add(parm1);
// }
// else
// comm.CommandText="select * from stu,dep where stu.studepid=dep.depid";
// SqlDataAdapter da=new SqlDataAdapter();
// da.SelectCommand=comm;
// DataSet ds=new DataSet();
// da.Fill(ds,"table1");
// this.DataGrid1.DataSource=ds.Tables["table1"];
// this.DataGrid1.DataBind();
//
// }
// #region Web 窗体设计器生成的代码
// override protected void OnInit(EventArgs e)
// {
// //
// // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
// //
// InitializeComponent();
// base.OnInit(e);
// }
//
// /// <summary>
// /// 设计器支持所需的方法 - 不要使用代码编辑器修改
// /// 此方法的内容。
// /// </summary>
// private void InitializeComponent()
// {
// this.Load += new System.EventHandler(this.Page_Load);
//
// }
// #endregion
//
// private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
// {
// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
// SqlDataAdapter da=new SqlDataAdapter("select * from dep",conn);
// DataSet ds=new DataSet();
// da.Fill(ds,"table1");
// if(e.Item.ItemType==ListItemType.EditItem)
// {
// DropDownList ddl=(DropDownList)e.Item.FindControl("dep");
// ddl.DataSource=ds.Tables["table1"];
// ddl.DataTextField="depname";
// ddl.DataValueField="depid";
// ddl.DataBind();
// ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;//选择数据库内的作为默认
// }
// }
//
// protected void edit(object sender,DataGridCommandEventArgs e)
// {
// this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
// if(this.DropDownList1.SelectedValue=="")
// SetBind();
// else
// SetBind3();
// }
//
// protected void cancel(object sender,DataGridCommandEventArgs e)
// {
// this.DataGrid1.EditItemIndex=-1;
// if(this.DropDownList1.SelectedValue=="")
// SetBind();
// else
// SetBind3();
// }
//
// protected void update(object sender,DataGridCommandEventArgs e)
// {
// if(e.Item.ItemType==ListItemType.EditItem)//只有在编辑按下以后才能提交
// {
// SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
// SqlCommand comm=new SqlCommand("update stu set stuname=@name,studepid=@depid where stuid=@id",conn);
// SqlParameter parm1=new SqlParameter("@name",SqlDbType.NVarChar,50);
// parm1.Value=((TextBox)e.Item.FindControl("name")).Text;
// SqlParameter parm2=new SqlParameter("@depid",SqlDbType.Int);
// parm2.Value=((DropDownList)e.Item.FindControl("dep")).SelectedValue;
// SqlParameter parm3=new SqlParameter("@id",SqlDbType.Int);
// parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
// comm.Parameters.Add(parm1);
// comm.Parameters.Add(parm2);
// comm.Parameters.Add(parm3);
// conn.Open();
// comm.ExecuteNonQuery();
// conn.Close();
// this.DataGrid1.EditItemIndex=-1;
// if(this.DropDownList1.SelectedValue=="")
// SetBind();
// else
// SetBind3();//如果选择过滤则使用SetBind3()
// }
// }
//
// private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
// {
// SetBind3();
// }
// }
//}