2个用于gridview的数据源 [英] 2 datasources for gridview

查看:62
本文介绍了2个用于gridview的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个网格视图,它在页面load上显示表中的数据.现在有一个搜索按钮,当单击该按钮时会显示文本框,并且可以输入文本来搜索数据.当您单击go时,同一网格视图必须显示搜索结果.因此,我在按钮上编写了ado.net代码,将gridview的数据源设置为dt(数据表),但是当我运行它时,我得到一个错误,说两者数据源n数据源id被赋予n以删除1.数据源id是数据库表中的数据,页面加载时来自该数据库表.

解决方案

您不能同时拥有2个数据源.为什么不对数据表使用LINQ查询,并从该查询中提取一个新的集合或数据表,然后将其用作gridview的数据源.绑定您的gridview,然后在按钮上单击您将Gridview与DataSource绑定?

1.一个简单的选项是在单击按钮时将datasourceid设置为null.如下所示:
GridView1.DataSourceID = null;
2.在这两种情况下,其他选择都可以是将gridview从背后的代码绑定到数据源.在页面加载时将数据存储在ViewState(或会话)中,并绑定您的gridview,稍后在单击按钮时,根据您的搜索条件从viewstate/session中获取数据并对其进行过滤.


Hi,
I have a grid view and it shows the data from a table on page load.now there is a search button which when clicked shows textboxes and one can enter text by which they want to search the data . when you click go ,the same grid view must show the search results.So i wrote ado.net code on the buton click making the data source for gridview as dt(data table) but when i run it i get an error saying that both datasource n datasourceid are given n to remove 1. the datasourceid is the database table from which the data comes on pageload. how can i supply 2 data sources for the same grid view in order to achieve this?

解决方案

You cannot have 2 data sources at the same time. Why not use a LINQ query on your datatables and from that query extract a new collection or data table which you then use as a data source to the gridview.


Is it because you use first datasourceid to bind your gridview and then on button click you bind the gridview with DataSource?

1. One easy option is to set datasourceid to null on button click. Something like below:
GridView1.DataSourceID = null;
2. Other option can be to bind your gridview to datasource from codebehind in both the cases. Store the data in the ViewState (or session) in page load and bind your gridview, later when you click on button, fetch the data from viewstate/ session filtering it based on your search criteria.


这篇关于2个用于gridview的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆