Making GridView respond to 2 DropDownLists
Hi there.
I have got 2 Drop Down Lists on my Form and 1 GridView. I want GridView to display the data according to selection from the Drop Down Lists.
For Example, One Drop Down List contains Names and other contains Dates. Both the Drop Down Lists can post back. So if i select a name from 1st Drop Down list, the Grid View should show all the results according to that Name. Similarly if i select the Date from the other Drop Down List , the Grid View should show the results according to the dates. But i cant figure out as how to bind Grid View to respond to 2 Drop Down List.
BTW i am binding both the Drop Down Lists and the Grid View to the DataSource Objects, which is getting data from the database.
Any Suggestions??
Re: Making GridView respond to 2 DropDownLists
Hey,
Rather than repeat the content, have a look at this walkthrough:
http://www.asp.net/(S(ywiyuluxr3qb2d...ial-07-vb.aspx
This should get you the majority of the way there.
Post back if you have any other questions.
Gary
Re: Making GridView respond to 2 DropDownLists
thanks for the link, but this is for one Drop Down List and a Grid View, i have got two different drop down lists which are getting the data from the database and then if value of any of the drop down list is changed, the gridview shows the resulting data.
I want gridview to respond to both Drop Down Lists.
Re: Making GridView respond to 2 DropDownLists
Hey,
That is why I said that that link would get you "most" of the way there. Start out with getting the first DDL working with the GridView. One you have gone through that process, it should be reasonably simple to extend it to using both.
Small steps.
Gary
Re: Making GridView respond to 2 DropDownLists
Thanks.
I already got one of the DDL and GridView working in collaboration with each other. I have also populated the other DDL with the data from my database using DataSource but dont know how to make Gridview respond to both of them
Re: Making GridView respond to 2 DropDownLists
Your datasource object should have something like control parameters. So the name property in your datasource can correspond to the first DDL and the date to the other DDL. On each postback of the DDL, the control parameters should be able to read what's in the DDL and reload the grid's data according to that. (This takes care of both DDLs feeding the grid). You'll also probably want the DDLs to have a separate datasource and not mix it up with the grid's datasource. Post your code if you're still stuck.
Re: Making GridView respond to 2 DropDownLists
Re: Making GridView respond to 2 DropDownLists
This is what i have already achieved.
I have 5 functions using tableadapters with dataset, all done visually using the wizard in Visual Web Developer.
Function1: it displays all the records from the database.
Function2: it takes all the distinct names from the database.
Function3: it takes a particular name and display all the records according to that names.
Function4: it takes distinct dates from the database (date is also nvarchar)
Function5: it takes a particular date and displays the record according to that date.
Here is what i have with controls
* i bind Function2 to DDL1, so it gets populated with distinct Name.
* i bind Function4 to DDL2, so it gets populated with distinct date.
* i bind Function3 to GridView, with parameter source as control (DDL1) in this case, so when i change the value in DDL1, the records change accordingly in GridView (This one is working great currently)
All the above stuuf is done, but i dont know how to make GridView respond , if i change the value in DDL2. One way is to bind the Gridview in such a way that Parameter source is DDL2, but then it wont respond to DDL1.
Re: Making GridView respond to 2 DropDownLists
BTW here is the code
Code:
<asp:DropDownList ID="DropDownList1" runat="server" Height="23px" Width="154px"
AutoPostBack="True" DataSourceID="AlKaramDatesDataSource" DataTextField="Date"
DataValueField="Date" style="margin-left: 11px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" Height="23px" Width="154px"
AutoPostBack="True" style="margin-left: 11px"
DataSourceID="AlKaramBanksDataSource" DataTextField="BankName"
DataValueField="BankName">
</asp:DropDownList>
<asp:ObjectDataSource ID="AlKaramBanksDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetAlKaramBankNames"
TypeName="SharjahDataSetTableAdapters.AlKaramBankInfo3TableAdapter">
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="AlKaramDatesDataSource" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetAllAlKaramDates"
TypeName="SharjahDataSetTableAdapters.AlKaramBankInfo2TableAdapter">
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="SharjahDataSource" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByDate"
TypeName="SharjahDataSetTableAdapters.AlKaramBankInfo1TableAdapter"
DeleteMethod="Delete" InsertMethod="Insert" UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="BankName" Type="String" />
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="Amount" Type="Int32" />
<asp:Parameter Name="Date" Type="String" />
<asp:Parameter Name="Remarks" Type="String" />
<asp:Parameter Name="Expr1" Type="String" />
<asp:Parameter Name="Original_ID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Date"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="BankName" Type="String" />
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="Amount" Type="Int32" />
<asp:Parameter Name="Date" Type="String" />
<asp:Parameter Name="Remarks" Type="String" />
<asp:Parameter Name="Expr1" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" Style="margin-left: 10px; margin-top: 27px"
Width="902px" Height="135px" AllowPaging="True"
AutoGenerateColumns="False" CssClass="GridViewStyle"
GridLines="None" AllowSorting="True" DataSourceID="SharjahDataSource"
Font-Names="Calibri">
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="BankName" HeaderText="BankName"
SortExpression="BankName" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks"
SortExpression="Remarks" />
</Columns>
<FooterStyle BackColor="Gray" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
Both bank Name and date are nvarchar