Solved

Populating GridView from a dropdown list.

Posted on 2006-07-21
3
1,576 Views
Last Modified: 2012-06-21
I have a GridView where I configured the SmartTag connection and SQL statement to use the value specified in the dropdown list control.  The query test worked correctly.  I added a data bind for the grid when the value in the dropdown changes, but this does not work and my grid does not appear.  Any thoughts on what might be wrong?

Protected Sub ddlSite_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSite.SelectedIndexChanged
        gvSiteDocuments.DataBind()
    End Sub

I am looking for some guidance or sample code that will help me complete this.  Any information will
be greatly appreciated.  Thanks.  DC900

My Environment
    Windows XP Pro
    Visual Studio 2005 & VB.NET
    SQL Server 2000
0
Comment
Question by:dc900
  • 2
3 Comments
 
LVL 5

Expert Comment

by:jjaqua
ID: 17155160
How are you populating the GridView i.e. what is the datasource? I would think you would have your drop down list change something in the query used for the GridViews datasource. In that case you would have to set the new data source and bind to the GridView when the drop down list changes.

        Me.GridView1.DataSource = NewDataSource(New Query based on selected drop down value)
        Me.GridView1.DataBind()
0
 
LVL 1

Accepted Solution

by:
BoggyBayouBoy earned 300 total points
ID: 17158658
If you are using a SQLDataSource... this would do the trick.  Make sure "AutoPostback" is true on your dropdownlist.  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True"
                    SortExpression="OrderID" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [OrderID], [CustomerID] FROM [Orders] WHERE ([CustomerID] = @CustomerID)" >
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue"
                    Type="String" />
            </SelectParameters>
                 
        </asp:SqlDataSource>
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
            DataTextField="ContactName" DataValueField="CustomerID" AutoPostBack="True">
        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CustomerID], [ContactName] FROM [Customers]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
0
 
LVL 1

Expert Comment

by:BoggyBayouBoy
ID: 17175072
Or... you can go to the ASP.NET Quickstart Tutorial Site and you'll find an example for doing just what you're asking.  

http://www.asp.net/QuickStart/aspnet/

look for the example called "VB Filter By DropDownList".  You'll find it under "Performing Data Access > Filtering Data"

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why don't I see this table in EDMX file? 2 23
Calling stored proc in EDMX 11 23
Runtime Error 2 27
Server Error 11 47
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now