Solved

Populating GridView from a dropdown list.

Posted on 2006-07-21
3
1,582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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