2016-05-24 106 views
0

我在显示数据时存在问题,这取决于DropDownList中的选定项目。我有一个表,例如一个equipment列和一个索引列primekey。 DDL只有不同的值,我想在新的gridview中显示所有特定的选定数据。根据DropDownList中的SelectedItem显示网格视图数据(asp.net/c#)

我的代码看起来是这样的:

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(); 
} 

回答

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值是整型

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

非常感谢您的回复,但现在它说这行中有一个错误:'datapter.Fill(dset);' 您是否有符合我的标准的示例代码:/? –

+0

这可能会有所帮助。发布另一个答案。 –

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(); 
    } 
+0

非常感谢队友为你的努力,但它似乎并没有这样工作:/。我不知道为什么。如果我在'OnSelectedIndexChanged_DropDownList1'函数中注释'GridViewBind();'函数我没有得到任何错误,但也没有数据。 在你的版本中,''GridViewBind();''Page-Load'中的''我得到了所有的数据:/。 也许它可以帮助你解决这个问题:/ 但我只是想通过从DDL中选择它来获取数据.. :) –

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和DROPDOWNLIST为更具体的东西像GridViewEquipment或类似的东西

+0

是的:/我不幸,或者它是错的? –

+0

不,你应该拥有它真的我只是以为你有错误 – ChenChi

+0

是的,我认为如此。如果我评论'GridViewBind();'出'OnSelectedIndexChanged_DropDownList1'函数我不会得到任何错误,但也没有数据。在你的版本中,注释'GridViewBind();'离开页面加载我只是得到所有的数据:/。也许它可以帮助你解决这个问题:/但我只是想通过从DDL中选择数据来获取数据.. :) –