[英]How to change the DataSource on a GridView (C#, ASP.Net)
I was wondering if any of you all knew how to change the DataSource on a gridview from the code behind. 我想知道是否所有人都知道如何从后面的代码更改gridview上的DataSource。 I have a GridView (gdvCars), and a dropdownlist that has all of the models listed.
我有一个GridView(gdvCars),以及一个列出所有模型的下拉列表。 When a model is selected, the user presses a button.
选择模型后,用户按下按钮。 When the button is pressed, it will switch data sources according to what was selected.
按下按钮后,它将根据所选内容切换数据源。 What I am having trouble with, is figuring out the correct syntax to switch the dataSource that my GridView is using.
我遇到的麻烦是找出正确的语法来切换GridView使用的dataSource。 How do you Unbind the first dataSource, and switch to a new one?
如何取消绑定第一个数据源,并切换到新的数据源?
Here's an example: 这是一个例子:
In your page: 在您的页面中:
<asp:DropDownList id="drp"
runat="server">
<asp:ListItem Value="GridViewDataSource1">Name 1</asp:ListItem>
<asp:ListItem Value="GridViewDataSource2">Name 2</asp:ListItem>
<asp:ListItem Value="GridViewDataSource3">Name 3</asp:ListItem>
</asp:DropDownList>
<asp:sqldatasource id="GridViewDataSource1" selectcommand="SELECT .. FROM .. " connectionstring="..." runat="server"/>
<asp:sqldatasource id="GridViewDataSource2" selectcommand="SELECT .. FROM .. " connectionstring="..." runat="server"/>
<asp:sqldatasource id="GridViewDataSource3" selectcommand="SELECT .. FROM .. " connectionstring="..." runat="server"/>
In code behind: 在后面的代码中:
void button_click(Object sender, EventArgs e)
{
string SourceName = drp.SelectedValue;
gdvCars.DataSourceID = SourceName;
gdvCars.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.