根据DropDownList中的SelectedItem显示网格视图数据(asp.net / c#)

时间:2016-05-24 10:02:05

标签: c# asp.net gridview dropdownlistfor

我在根据DropDownList中的所选项目显示数据时遇到问题。我有一个表格,例如equipment - 列和索引列primekey。 DDL只有不同的值,我想在新的gridview中显示所有特定的选定数据。

我的代码如下:

public partial class _Default : Page
{
    OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["OLEDB"].ToString());
    OleDbDataAdapter datapter;
    DataSet dset;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string queryGV = "SELECT * FROM table time > current_timestamp -20  AND EQUIPMENT != 'sth' ORDER BY time DESC";
            string queryDLL1 = "SELECT DISTINCT EQUIPMENT FROM table ORDER BY EQUIPMENT";


            OleDbCommand cmdGV = new OleDbCommand(queryGV, conn);
            OleDbCommand cmdDLL1 = new OleDbCommand(queryDLL1, conn);

            DataSet ds = new DataSet();
            DataSet DLL1 = new DataSet();

            OleDbDataAdapter da = new OleDbDataAdapter(cmdGV);
            OleDbDataAdapter daDLL1 = new OleDbDataAdapter(cmdDLL1);

            da.Fill(ds);
            daDLL1.Fill(DLL1);

            GridView1.DataSource = ds;
            DropDownList1.DataSource = DLL1;

            GridView1.DataBind();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Teil", "0"));
            GridViewBind();

            conn.Close();
        }
    }

    protected void OnSelectedIndexChanged_DropDownList1(object sender, EventArgs e)
    {
        string valtxt = DropDownList1.SelectedItem.ToString();

        if (DropDownList1.SelectedIndex != 0)
        {
            GridViewBind();
        }
    }
}

此函数发生错误:

public void GridViewBind()
{
    datapter = new OleDbDataAdapter("select equipment, primekey from PI_EVENT_TABLE where primekey=" + DropDownList1.SelectedValue + "", conn);
    dset = new DataSet();
    datapter.Fill(dset);
    GridView1.DataSource = dset.Tables[0];
    GridView1.DataBind();
}

3 个答案:

答案 0 :(得分:0)

以下行中存在字符串格式错误:

datapter = new OleDbDataAdapter("select equipment, primekey from PI_EVENT_TABLE where primekey=" + DropDownList1.SelectedValue + "", conn);

像这样使用

如果primekey值是字符串类型

datapter = new OleDbDataAdapter("select equipment, primekey from PI_EVENT_TABLE where primekey="' + DropDownList1.SelectedValue + '", conn);

如果primekey值为int type

datapter = new OleDbDataAdapter("select equipment, primekey from PI_EVENT_TABLE where primekey=" + DropDownList1.SelectedValue.ToString(), conn);

答案 1 :(得分:0)

    OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["OLEDB"].ToString());
    OleDbDataAdapter datapter;
    DataSet dset;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string queryGV = "SELECT * FROM table time > current_timestamp -20  AND EQUIPMENT != 'sth' ORDER BY time DESC";
            string queryDLL1 = "SELECT DISTINCT EQUIPMENT FROM table ORDER BY EQUIPMENT";


            OleDbCommand cmdGV = new OleDbCommand(queryGV, conn);
            OleDbCommand cmdDLL1 = new OleDbCommand(queryDLL1, conn);

            DataSet ds = new DataSet();
            DataSet DLL1 = new DataSet();

            OleDbDataAdapter da = new OleDbDataAdapter(cmdGV);
            OleDbDataAdapter daDLL1 = new OleDbDataAdapter(cmdDLL1);

            da.Fill(ds);
            daDLL1.Fill(DLL1);

            GridView1.DataSource = ds;
            DropDownList1.DataSource = DLL1;

            GridView1.DataBind();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Teil", "0"));
            //GridViewBind();

            conn.Close();
        }
    }

    protected void OnSelectedIndexChanged_DropDownList1(object sender, EventArgs e)
    {
        string valtxt = DropDownList1.SelectedItem.ToString();

        if (DropDownList1.SelectedIndex != 0)
        {
            GridViewBind();
        }
    }

    private void GridViewBind()
    {
        string query = "select equipment, primekey from PI_EVENT_TABLE where primekey=" + DropDownList1.SelectedValue.ToString();
        conn.Open();
        datapter = new OleDbDataAdapter(query, conn);
        dset = new DataSet();
        datapter.Fill(dset);
        GridView1.DataSource = dset.Tables[0];
        GridView1.DataBind();
        conn.Close();
    }

答案 2 :(得分:0)

public void GridViewBind()
{
    string connectionString = ConfigurationManager.ConnectionStrings["OLEDB"].ToString();
    string selectCommand = "SELECT equipment, primekey FROM PI_EVENT_TABLE WHERE primekey = " 
    + DropDownList1.SelectedValue;

    DataTable table = new DataTable();

    using(var con = new OleDbConnection(connectionString))
    using(var command = new OleDbCommand(selectCommand, con))
    {
        con.Open();

        try
        {
           table.Load(command.ExecuteReader());
           GridView1.DataSource = table;
           GridView1.DataBind();
        }
        catch(Exception) // place break point here to see if you have exceptions
        {
           throw;
        }
    }
}

此外,您应该将gridview和下拉列表重命名为更具体的内容,例如GridViewEquipment或其他类似内容