Reputation: 6395
I have dropdownlist like this:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="22px" Width="650px">
<asp:ListItem> </asp:ListItem>
<asp:ListItem>spy</asp:ListItem>
<asp:ListItem>uk</asp:ListItem>
<asp:ListItem>it</asp:ListItem>
</asp:DropDownList>
And I use csv files for data. Like this:
spy.csv
uk.csv
it.csv
I can add 3 sqldatasources for 3 csv files and link gridview etc.
But I need to do, when I select spy on my dropdownlist, gridview use spydatasource, when I select uk, gridview use ukdatasource.
How can I do?
Datasources
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [spy.csv] WHERE ([Hostname] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Hostname"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [uk.csv] WHERE ([Hostname] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Hostname"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [it.csv] WHERE ([Hostname] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Hostname"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Upvotes: 0
Views: 1141
Reputation: 1314
This is going to be too much information to post into a comment, so I will write it in an answer. Please let me know if it is along the lines of what you are looking for.
Front-end:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="22px" Width="650px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem> </asp:ListItem>
<asp:ListItem>spy</asp:ListItem>
<asp:ListItem>uk</asp:ListItem>
<asp:ListItem>it</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridViewID" .... >
</asp:GridView>
Code-Behind
protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) {
if(DropDownList1.SelectedIndex != 0)
{
var csvFile = DropDownList1.SelectedValue + ".csv";
DataTable dt = //Convert csvFile to dataTable
GridViewID.DataSource= dt;
//This is an alternate method, setting the Select Command of a given SqlDataSource
SqlDataSourceID.SelectCommand = String.Format("SELECT * FROM [{0}] WHERE ([Hostname] = ?)",csvFile);
}
}
Here is a short guide I found on how to convert CSV to GridView Datasource; http://www.c-sharpcorner.com/UploadFile/hrojasara/how-you-can-use-csv-file-as-data-source-of-gridview/
EDIT: Added an alternative way of handling changing DropDownList.
Let me know if it helps!
Upvotes: 4
Reputation: 644
Something like this?
protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) {
if(DropDownList1.SelectedItem.Text == "1ofthevalues") { yourGridview.DataSource= yourDataSourceID yourGridview.DataBind(); } }
Oh and add {OnSelectedIndexChanged= "DropDownList1_SelectedIndexChanged"} to your dropdownlist
Upvotes: 1
Reputation: 11995
I'd go with Sander's comment.
Alternatively, I'd have three different data sources (each with different select commands). I'd have three connected to each correspondingly.
Based on my dropdown selection, I'd make the grid corresponding to the dropdown list's selected option visible.
This is a long shot for a small problem. But do-able simply via the designer only.
Upvotes: 1