Anurag Jain
Anurag Jain

Reputation: 1389

How to display selected data from dropdownlist in grid view?

I have a dropdownlist in which I want to show different branches of a college.

When a user selects one of the value from the dropdownlist then the data corresponding to that value has to be displayed in the grid view.

For example when a user selects "information technology" from drop-down list box then list of faculty members related to information technology has to be displayed in grid view.

Upvotes: 0

Views: 14298

Answers (3)

Nitin Aggarwal
Nitin Aggarwal

Reputation: 461

write your code on dropdown selected index changed method that will bind the data to the gridview by getting the value and set the dropdown property autopostback to true

.aspx file

<asp:DropDownList id="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_onSelectIndexChanged"/>
<asp:ListItem Value="1">Finance</asp:ListItem>
<asp:ListItem Value="2">Information Technology</asp:ListItem>
</asp:DropDownList>
<br/>
<br/>

<asp:GridView id="GridView1" runat="server">

Your c# Code

protected void ddlist_onSelectIndexChanged(object sender, EventArgs e)
 {
           string selectedBranch=ddlBranch.SelectedItem.Text;

           DataSet dsBranchDetails=GetDataForBranch(selectedBranch);

            GridView1.DataSource=dsBranchDetails;
            GridView1DataBind();
 }


public DataSet GetDataForBranch(string selectedBranch)
{
 //     your code
}

Upvotes: 1

Zo Has
Zo Has

Reputation: 13018

  1. Bind your gridview to a datasource
  2. Define a control parameter on your datasource (object datasource/SQL datasource etc.) & set the control parameter to your dropdown list
  3. Set AutoPostback property of your dropdown list to true

Now whenever you would select a value from dropdown the gridview would be populated respectively.

Upvotes: 0

Laurent S.
Laurent S.

Reputation: 6946

Here's how I would do it :

1) Use a global datatable variable to store the complete data. It will contain your whole database table (something like "Select * from facultymembers")

2) On load, use that variable to populate a gridview, aka your table in aspx. Use it also to build the list of items of you selectbox, by extracting distinct values of the college branches and binding it to the dropdownlist see this post for code

3) Attach an "onchange" event to your dropdownlist. In the implementation of that event, you'll catch the selected value and can use it to filter your datatable (which is global) using the rowfilter property of the default dataview. Once done, all you need is firing a new databind() , binding this filter data back to your gridview

Hope tis helps...

Upvotes: 0

Related Questions