从下拉列表中选择后,在gridview中显示数据

时间:2013-03-26 13:03:10

标签: asp.net sql-server gridview

enter image description here您好我只想在从下拉列表中选择值后在我的gridview中显示数据,并在选择我的下拉列表后按下搜索按钮数据时可以在gridview中看到。我的gridview与我的数据库连接我怎么能这样做我的代码是这样的:

aspx页面:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" 
        GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Portal" HeaderText="Portal" 
                SortExpression="Portal" />
            <asp:BoundField DataField="TID" HeaderText="TID" SortExpression="TID" />
            <asp:BoundField DataField="PNR" HeaderText="PNR" SortExpression="PNR" />
            <asp:BoundField DataField="TicketNumber" HeaderText="TicketNumber" 
                SortExpression="TicketNumber" />
            <asp:BoundField DataField="ESACCode" HeaderText="ESACCode" 
                SortExpression="ESACCode" />
            <asp:BoundField DataField="WaiverCode" HeaderText="WaiverCode" 
                SortExpression="WaiverCode" />
            <asp:BoundField DataField="Remarks" HeaderText="Remarks" 
                SortExpression="Remarks" />
            <asp:BoundField DataField="UnusedTicketAmount" HeaderText="UnusedTicketAmount" 
                SortExpression="UnusedTicketAmount" />
            <asp:BoundField DataField="ddlUnusedAmount" HeaderText="ddlUnusedAmount" 
                SortExpression="ddlUnusedAmount" />
            <asp:BoundField DataField="AirlinePenality" HeaderText="AirlinePenality" 
                SortExpression="AirlinePenality" />
            <asp:BoundField DataField="ddlAirlinePenality" HeaderText="ddlAirlinePenality" 
                SortExpression="ddlAirlinePenality" />
            <asp:BoundField DataField="NetRefundProcess" HeaderText="NetRefundProcess" 
                SortExpression="NetRefundProcess" />
            <asp:BoundField DataField="ddlNetRefundProcess" 
                HeaderText="ddlNetRefundProcess" SortExpression="ddlNetRefundProcess" />
            <asp:BoundField DataField="RefundableCommission" 
                HeaderText="RefundableCommission" SortExpression="RefundableCommission" />
            <asp:BoundField DataField="ddlRefundableCommission" 
                HeaderText="ddlRefundableCommission" SortExpression="ddlRefundableCommission" />
            <asp:BoundField DataField="CouponRefunded" HeaderText="CouponRefunded" 
                SortExpression="CouponRefunded" />
            <asp:BoundField DataField="RefundType" HeaderText="RefundType" 
                SortExpression="RefundType" />
            <asp:BoundField DataField="DateTime" HeaderText="DateTime" 
                SortExpression="DateTime" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BartConnectionString %>" 
        SelectCommand="SELECT * FROM [Test]"></asp:SqlDataSource>

aspx.cs页面上的代码

public void GridViewBind()
{
    SqlCommand cmd = new SqlCommand("select * from Test where ID=" +     DropDownList1.SelectedValue + "", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "Test");
    GridView1.DataSource = ds.Tables["Test"];
    GridView1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
    this.GridViewBind();
}

从我的下拉列表中选择值后错误:列名称'TRAVELONG'无效

1 个答案:

答案 0 :(得分:1)

ID是字符串吗?如果是这样,您需要ID字段的内部引号:

 SqlCommand cmd = new SqlCommand("select * from Test where ID='" +     DropDownList1.SelectedValue + "'", con);

否则,它可能会将所选值视为列...