0

hello everyone, this is my first post.

i'm working on something that seems quite simple but i don't have the know-how. i want to populate a gridview in a web application with a field selected from a drop down list.

basically, once you select an item is selected from a drop down, a postback happens and a grid view is populated with the field that contains that particular item.

i hope i've made myself clear enough, can anyone help me. thank you.

4
Contributors
14
Replies
17
Views
8 Years
Discussion Span
Last Post by tryongliph
0

Do you want selected item in the dropdownlist to be displayed in the gridview or fetch some item from a data source based on the selected item in the dropdownlist and display it in the gridview.

0

If you want to display the item in gridview with the item selected in dropdownlist on selected index change of dropdownlist, then here is your code to do this.:)

First declare a dropdownlist and Gridview in the aspx web page like shown below:

<div>
        <asp:DropDownList ID="ddItems" runat="server" Width="100px" OnSelectedIndexChanged="ddItems_SelectedIndexChanged" AutoPostBack="true">
            <asp:ListItem Text="10" Value="10"></asp:ListItem>
            <asp:ListItem Text="11" Value="11"></asp:ListItem>
            <asp:ListItem Text="12" Value="12"></asp:ListItem>
            <asp:ListItem Text="13" Value="13"></asp:ListItem>
        </asp:DropDownList>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>

Then for initial loading of the gridview and to load gridview with the item selected in the dropdownlist on selected index change of dropdownlist write the following code in the code-behind:

//Declare ArrayList object al in the class
ArrayList al;
    protected void Page_Load(object sender, EventArgs e)
    {
//For initial loading of gridview
        al = new ArrayList();
        al.Add("test");
        al.Add("test1");
        GridView1.DataSource = al;
        GridView1.DataBind();
    }
    protected void ddItems_SelectedIndexChanged(object sender, EventArgs e)
    {
//to load the gridview with the item selected in gridview
        al.Clear();
        al.Add(ddItems.SelectedValue);
        GridView1.DataSource = al;
        GridView1.DataBind();
    }

I think this will solve your problem. If not the contact me anytime.
If this solved your problem, please mark it solved. I hope you understand, it will be motivation for us.

Thanks,

0

Above code is very simple. If this is not the thing you want. Cheer up man, just let me know. I have other options as well.

0

Do you want selected item in the dropdownlist to be displayed in the gridview or fetch some item from a data source based on the selected item in the dropdownlist and display it in the gridview.

can you answer that

0

ok guys, this is what i want. i have a drop down (two actually) which is bound to an sql datasource. now the data source is picking from a database which the gridview is displaying. what i want is to be able to use the drop down to filter data based on the item selected. so if i pick say 'john' from a drop down, the gridview should display all records of john.
here's a snippet of the code behind:

private SqlConnection Fac_Connection;
    private SqlCommand Fac_Command;
    private SqlDataAdapter Fac_Adapter;
    private DataSet Fac_DataSet;
    private DataTable Fac_DataTable;
        
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Fac_Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ToString());
        Fac_Command = new SqlCommand();
        Fac_Command.Connection = Fac_Connection;
        Fac_Adapter = new SqlDataAdapter();
        Fac_DataSet = new DataSet();
      
    }

    protected void FacLocation_Dd_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fac_Command.CommandText = "SELECT * FROM Facility WHERE Location='" + FacLocation_Dd.SelectedValue.Trim() + "'";
        Fac_Adapter.SelectCommand = Fac_Command;
        Fac_Adapter.Fill(Fac_DataSet);
        Fac_DataTable = Fac_DataSet.Tables[0];

        Fac_GridView.DataSource = Fac_DataTable;
        Fac_GridView.DataBind();
    }

also i'm working with uniqueidentifier in my sqldbase and i want to know how to use a for statement or while in the query so i can automate the filling in of data. thanks guys

0

what exactly are you referring to. You already got your gridview filled with the record's based on the filter value from the dropdownlist.

Are you looking for looping in sql or in c#.

Sorry man, i am confused.

0

ok, first: this code seems to work alright but when i run it and select a value, the gridview doesn't show. nothing displays in the page though the page executes a post-back.

secondly: i was asking if anyone knew how to do a loop in sql, be it for or while. i want to fill in the data table with a sequential data so i want to loop it for say 10 rows but i don't know how. i'm using sql server 2008.

i hope this clearifies issues, thanks for your patience guys.

0

I copied and run your code on my computer. It works fine on my computer may be your database doesn't consist of data which is extracted on the basis of filter from dropdownlist.

0

And do you mean looping through the resultset returned that is returned from the database or looping in sql statement.

0

o ok, hmm, will check that, thanks. uhm yeah, looping in sql statement.

0

hey guys, i think i solved the problem by hardcoding the list of items in the drop down. also i have decided to go a further step by adding a second datasource and linking the grid table to that one instead. thanks guys for your help

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.