Solved

Populating GridView from a dropdown list.

Posted on 2006-07-21
3
1,574 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

17 Experts available now in Live!

Get 1:1 Help Now