How to change the DataSource on a GridView (C#, ASP.Net) -


i wondering if of knew how change datasource on gridview code behind. have gridview (gdvcars), , dropdownlist has of models listed. when model selected, user presses button. when button pressed, switch data sources according selected. having trouble with, figuring out correct syntax switch datasource gridview using. how unbind first datasource, , switch new one?

here's example:

in 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 .. .. " connectionstring="..." runat="server"/> <asp:sqldatasource id="gridviewdatasource2" selectcommand="select .. .. " connectionstring="..." runat="server"/> <asp:sqldatasource id="gridviewdatasource3" selectcommand="select .. .. " connectionstring="..." runat="server"/> 

in code behind:

void button_click(object sender, eventargs e) {     string sourcename = drp.selectedvalue;     gdvcars.datasourceid = sourcename;     gdvcars.databind(); } 

Comments