根据下拉列表选择sql server asp.net过滤数据

时间:2015-07-13 12:06:06

标签: c# asp.net drop-down-menu filter

我一直在努力解决这个问题。这就是我想要实现的目标:根据下拉列表的选择,我不会在gridview上看到只有与该特定列相等的结果。该表有3列F1 F2和F3。我想在F2上过滤,然后能够将该表作为csv文件下载。不幸的是,这些功能目前都没有。

以下是布局代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="ddlcountry" runat="server" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList>
        <br />
        <br />
        <br />


        <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
       <asp:Button id="btnExportCSV" Text="Download" runat="server" />
    </form>
</body>
</html>

这是代码。

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

{

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadGridview();
            LoadCountries();

        }}

    private void LoadCountries()
    {
        ddlcountry.DataSource = LoadData();


        ddlcountry.DataValueField = "F2";
        ddlcountry.DataTextField = "F2";
        ddlcountry.DataBind();

    }

    private void LoadGridview()
    {
        GridView1.DataSource = LoadData();
        GridView1.DataBind();
    }

    private void LoadGridview(String id)
    {
        GridView1.DataSource = LoadData(id);
        GridView1.DataBind();
    }


    private DataSet LoadData()
    {
        SqlDataAdapter adap = new SqlDataAdapter("select * from [Parts$]", conn);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        return ds;
    }
    private DataSet LoadData(String id)
    {
        SqlDataAdapter adap = new SqlDataAdapter("select * from [Parts$] where F2= " +id+ "", conn);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        return ds;
    }

    protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadGridview(ddlcountry.SelectedValue);
    }



   public override void VerifyRenderingInServerForm(Control control)
    {
        /*Tell the compiler that the control is rendered
         * explicitly by overriding the VerifyRenderingInServerForm event.*/
    }

    protected void btnExportCSV_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        StringBuilder sBuilder = new System.Text.StringBuilder();
        for (int index = 0; index < GridView1.Columns.Count; index++)
        {
            sBuilder.Append(GridView1.Columns[index].HeaderText + ',');
        }
        sBuilder.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.HeaderRow.Cells.Count; k++)
            {
                sBuilder.Append(GridView1.Rows[i].Cells[k].Text.Replace(",", "") + ",");
            }
            sBuilder.Append("\r\n");
        }
        Response.Output.Write(sBuilder.ToString());
        Response.Flush();
        Response.End();
    }
}

1 个答案:

答案 0 :(得分:0)

将查询中的数据加载到id = @id的位置,然后将第二个参数作为传递给方法的id。最后你的康恩。