Solved

Populating GridView from a dropdown list.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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