C# 怎样利用下拉列表对GridView进行动态查询?

xi_ye 2009-04-15 04:47:42
有多个下拉列表,动态对下拉列表进行绑定,
希望下拉列表改变之后,GridView同时更新。

注意,这几个下拉列表之间没有联动,都是独立的。

如: 部门: 片区:
这是两个DropDownList,
1.如何这对两个进行动态的绑定?
2.当选择下拉列表里不同的值,下面的GridView同时更新?

求前台与后台代码,c#的。谢谢。
我用的是VS2008,sql2005.
...全文
869 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
heshunneng 2010-07-30
  • 打赏
  • 举报
回复
AuToPostBack=true即可
xi_ye 2009-04-20
  • 打赏
  • 举报
回复
我的确是女生!非常感谢大家!现在问题已解决!
snoopy83101 2009-04-16
  • 打赏
  • 举报
回复
DropDownList绑定数据后.把属性里的AutoPostBack设置为True

然后绑定Gridview 在配置sqldatasourse数据控件的时候,选择where条件,条件中选择获取DropDownList的SelectVlaue值就行.

以上功能只用鼠标就能实现,不用键盘.

如果能实现了.写代码肯定更没问题.试试吧.

如果有两个DropDownList,弄两个gridview数据源.
如果这个DropDownList被change了
this.Gridview1.datasourse=第一个数据源
this.Gridview1.databind();

如果第二个DropDownList被change了
this.Gridview1.datasourse=第二个数据源
this.Gridview1.databind();

哈哈.最后说下.以上都是我YY的.
Vivian.Lee 2009-04-16
  • 打赏
  • 举报
回复
前台没有什么需要写的,主要是在后太,你在下拉列表中写个点击事件 protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e),在这个事件中绑定你的写个函数绑定你的GridView。每次点击更换下拉列表。GridView就会刷新。

  • 打赏
  • 举报
回复
guidview+ObjectDataSource+SelectParameters

我觉得是比较好的方式
bao_yan 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xi_ye 的回复:]
14楼的朋友,非常感谢!
但是你没有理解我的意思,可能是我表达不清的原因!
员工名,和福利信息是显示在GridView里的,下拉列表里分别是显示的"部门名","片区名";
部门名,片区名都不是同一张表,它两者没有关系,但部门ID,片区ID是员工表里的外键,
我想的是怎样动态的进行绑定下拉列表里的部门,与片区,
如:当点击部门名1里,GridView里就会把部门1里面的员工1的福利信息显示在GridView里
[/Quote]

MM,

按这种流程做就好了 ..

"部门名"这个DDL 设置下AutoPostBack="true" ,绑定一个 onselectedindexchanged
在这个事件里面给GridView进行重新绑定.
你可以给这个GridView写个公用的绑定方法
比如
public void GridViewBind(string 下拉列表变的时候传入的参数,string 标识是部门列表传入的参数还是区间传入的参数)
{
ArrayList list=null;
if(标识是部门传入的参数)
{
把第一个参数传入到你要根据部门为参数查询的SQL语句当中,把数据放到list里
}
else
{
把第一个参数传入到你要根据区间为参数查询的SQL语句当中,把数据放到list里
}
在这里绑定GridView
GridView.DataSource = list;
GridView.DataBind();
}

"部门名"DropDownList 设置下AutoPostBack="true" ,绑定一个 onselectedindexchanged 这个事件只需要调用一下GridViewBind(当前选中的项)
区间也是同样的做法.

楼主肯定是女生
凤宝儿 2009-04-16
  • 打赏
  • 举报
回复
调用DropDownList的Onchange...事件
重新对Gridview1绑定下dt或者ds不就OK了么?
皓月明 2009-04-16
  • 打赏
  • 举报
回复
靠楼上的人都粘那么多代码,眼花缭乱
xi_ye 2009-04-15
  • 打赏
  • 举报
回复
14楼的朋友,非常感谢!
但是你没有理解我的意思,可能是我表达不清的原因!
员工名,和福利信息是显示在GridView里的,下拉列表里分别是显示的"部门名","片区名";
部门名,片区名都不是同一张表,它两者没有关系,但部门ID,片区ID是员工表里的外键,
我想的是怎样动态的进行绑定下拉列表里的部门,与片区,
如:当点击部门名1里,GridView里就会把部门1里面的员工1的福利信息显示在GridView里
levinstong 2009-04-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 CloudOfFly 的回复:]
你建立一个部门表
dptID Name Area

一个和部门表关联的信息表
ID ,dpt, 销售数量,销售额,
写一个依据dtp获取数据的方法
private dateSet GetdateById(int id)
{
//这里就是执行SQL获取数据 以DataSet的形式返回数据
}

然后你在页面把部门表绑定到DropDownList里面

然后在他的SelectedIndexChanged事件里面写
获取他的当前选种索引 SelectedItem这个获取
例如: int id = this.ddbooks.SelectedI…
[/Quote]
zzxap 2009-04-15
  • 打赏
  • 举报
回复
[code=C#]
STRING SQL=" SELECT FROM TABLE1 A LEFT JOIN TABLE2 B ON A.ID=B.ID WHERE 1=1 "
IF (DropDownList1.selectvalue!= "")
{
SQL+=" and A.员工名 like '%"+DropDownList1.selectitem.text +"'"

}
IF (DropDownList2.selectvalue!= "")
{
SQL+=" and B.福利信息 like '%"+DropDownList2.selectitem.text +"'"

}

自己变通一下吧
[/CODE]
阿彪兄 2009-04-15
  • 打赏
  • 举报
回复
启用dropdownlist的autopostback,在selectedindexchanged事件中写程序
xi_ye 2009-04-15
  • 打赏
  • 举报
回复
这两下拉都是动态获取数据,并且选择下拉里的数据里(这两个不是在同一张表里),GridView将更新(显示员工的福利信息,有员工名,福利信息,而下拉里的数据表的ID是员工表的外键)
xi_ye 2009-04-15
  • 打赏
  • 举报
回复
8楼的那位:这两下拉都是在不同的表里的,不是在同一张表里!
zzxap 2009-04-15
  • 打赏
  • 举报
回复
STRING SQL=" SELECT * FROM TABLE WHERE 1=1 "
zzxap 2009-04-15
  • 打赏
  • 举报
回复
DropDownLis2 的autopostback=true

然后双击DropDownLis2在后台生成的方法里调用getdate();
zzxap 2009-04-15
  • 打赏
  • 举报
回复
[code=C#]
先拼好SQL
private void getdate()
{
STRING SQL=" SELECT FROM TABLE WHERE 1=1 "
IF (DropDownList1.selectvalue!= "")
{
SQL+=" and a like '%"+DropDownList1.selectitem.text +"'"

}
IF (DropDownList2.selectvalue!= "")
{
SQL+=" and a like '%"+DropDownList2.selectitem.text +"'"

}
string strl=" server=xx ;database=xx ;userid=xx;pwd=xx"
sqlconnection conn=new sqlconnection(str1 )
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "head");
this.gridview1.DataSource = ds;
this.gridview1.DataBind();
//绑定gridview
}


然后Page_Load调用
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdate();

}
}
[/CODE]
xi_ye 2009-04-15
  • 打赏
  • 举报
回复
5楼的那位:我有两个问题,
1.如何动态绑定下拉列表,(下拉列表里的值是直接从数据里提取出来)?
2.绑定好了后,当我们选择下拉列表里的值后,相应的gridview里的值随之更新(下拉列表里的值不在Gridview里显示,根据下拉列表里的值的ID来显示其它信息)?
这两个问题?各位帮帮忙.要不在6楼我的代码里看看能不能帮我修改一下.谢谢!
xi_ye 2009-04-15
  • 打赏
  • 举报
回复
前台代码:

部门 :<asp:DropDownList ID="DropDownList_Department" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList_Department_SelectedIndexChanged">
</asp:DropDownList>
片区: <asp:DropDownList ID="DropDownList_Area" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList_Area_SelectedIndexChanged">
</asp:DropDownList>

后台:
        #region Page_Load事件
protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = -100;
if (!IsPostBack)
{
//绑定部门
DepartmentBind();
//绑定片区
AreaBind();
this.TextBox_Name.Focus();

ViewState["SortOrder"] = "WelfarePayRecord_ReceiveDatestamp";
ViewState["OrderDire"] = "DESC";
WelfarePayRecordBind();
}
}
#endregion

#region 部门绑定
protected void DepartmentBind()
{
IDepartmentManage _IDepartmentManage = new DepartmentManage();
HR_DepartmentDataSet _HR_DepartmentDataSet= new HR_DepartmentDataSet();
IList<HR_DepartmentDataSet.HR_DepartmentRow> _HR_DepartmentRows = new List<HR_DepartmentDataSet.HR_DepartmentRow>();
HR_DepartmentDataSet.HR_DepartmentRow _HR_DepartmentRow = _HR_DepartmentDataSet.HR_Department.NewHR_DepartmentRow();
_HR_DepartmentRows.Add(_HR_DepartmentRow);

this.DropDownList_Department.DataSource = _HR_DepartmentDataSet.Tables["HR_Department"].DefaultView;
this.DropDownList_Department.DataTextField = "Name";
this.DropDownList_Department.DataValueField = "ID";
this.DropDownList_Department.DataBind();
this.DropDownList_Department.Items.Add("----请选择----");
this.DropDownList_Department.SelectedValue = "----请选择----";

}
#endregion

#region 片区绑定
protected void AreaBind()
{
IAreaManage _IAreaManage = new AreaManage();
HR_AreaDataSet _HR_AreaDataSet = new HR_AreaDataSet();
IList<HR_AreaDataSet.HR_AreaRow> _HR_AreaRows = new List<HR_AreaDataSet.HR_AreaRow>();
HR_AreaDataSet.HR_AreaRow _HR_AreaRow = _HR_AreaDataSet.HR_Area.NewHR_AreaRow();
_HR_AreaRows.Add(_HR_AreaRow);

this.DropDownList_Area.DataSource = _HR_AreaDataSet.Tables["HR_Area"].DefaultView;
this.DropDownList_Area.DataTextField = "Name";
this.DropDownList_Area.DataValueField = "ID";
this.DropDownList_Area.DataBind();
this.DropDownList_Area.Items.Add("----请选择----");
this.DropDownList_Area.SelectedValue = "----请选择----";
}
#endregion
#region 绑定福利发放基本信息
protected bool WelfarePayRecordBind()
{
if (m_Flag)
{
IWelfarePayRecordManage _IWelfarePayRecordManage = new WelfarePayRecordManage();
HR_WelfarePayRecordListDataSet _WelfarePayRecordListDataSet = new HR_WelfarePayRecordListDataSet();
HR_WelfarePayRecordListDataSet.HR_WelfarePayRecordRow _WelfarePayRecordRow = _WelfarePayRecordListDataSet.HR_WelfarePayRecord.NewHR_WelfarePayRecordRow();

if (!this.DropDownList_Department.SelectedValue.Equals("----请选择----"))
{
_WelfarePayRecordRow.Department_Name = Convert.ToString(this.DropDownList_Department.SelectedValue);
}
if (!this.DropDownList_Area.SelectedValue.Equals("----请选择----"))
{
_WelfarePayRecordRow.BelongArea_Name = Convert.ToString(this.DropDownList_Area.SelectedValue);
}

_WelfarePayRecordListDataSet = _IWelfarePayRecordManage.SearchWelfarePayRecord(_WelfarePayRecordRow);
DataView _DataView = _WelfarePayRecordListDataSet.HR_WelfarePayRecord.DefaultView;
string _Sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
_DataView.Sort = _Sort;
GridView_HRWelfarePayRecord.DataSource = _DataView;
GridView_HRWelfarePayRecord.DataBind();

}
return true;
}
#endregion

#region 部门下拉列表事件
protected void DropDownList_Department_SelectedIndexChanged(object sender, EventArgs e)
{
WelfarePayRecordBind();
}
#endregion

#region 片区下拉列表事件
protected void DropDownList_Area_SelectedIndexChanged(object sender, EventArgs e)
{
WelfarePayRecordBind();
}
#endregion


"绑定福利发放基本信息"里我不知道该怎样去绑定下拉列表,我那样是正确的吗?
还有,我是做的B/S的.各位帮我看看.谢谢!
liguoliangaccp 2009-04-15
  • 打赏
  • 举报
回复
没太明白你的需求,下拉列表是要绑定到GridView的列吗?
如果不是这样的就更简单了,只要在listbox的更改选定内容事件中更新dataSet然后刷新就行了
我绑定的是一个列表!代码如下,这样是你定制的视图,当你鼠标点击绑定控件的列的时候才会显示下拉框,省的通篇全是下拉框,太难看
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
try
{
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;
//这里是你要将组件绑定的位置(第三列)
if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
{
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;
this.comboBox1.Left = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Left;
this.comboBox1.Top = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Top;
this.comboBox1.Width = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Width;
string ffff = Convert.ToString(this.dataGridView1.CurrentCell.Value);
this.comboBox1.Text = ffff;
this.comboBox1.Visible = true;
}
else
{
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}

}

private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
this.comboBox1.Visible = false;
this.comboBox1.Width = 0;

}
加载更多回复(4)

62,267

社区成员

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

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

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

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