2014-06-24 49 views
0

我需要能够从组合框或文本框中选择一个员工,并使其适用于我的datagridview,因此我只能看到选定的员工。下面是我的代码,目前用于从表中选择所有内容。有任何想法吗?当你从组合框或文本框中选择时创建一个datagridview

//Report groupbox - load groupbox 
    private void groupBox7_Enter(object sender, EventArgs e) 
    { 
     //Load Username 
     using (OleDbConnection con = new OleDbConnection(constring)) 
     { 
      try 
      { 
       string query = "SELECT TellerNum FROM Employee ORDER BY TellerNum ASC"; 
       OleDbDataAdapter da = new OleDbDataAdapter(query, con); 
       con.Open(); 
       DataSet ds = new DataSet(); 
       da.Fill(ds, "Name"); 
       comboBox20.DisplayMember = "TellerNum"; 
       comboBox20.DataSource = ds.Tables["Name"]; 
       con.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
     using (OleDbConnection con = new OleDbConnection(constring)) 
     { 
      this.sESSIONTableAdapter.Fill(this.trainingDBDataSet5.SESSION); 
      try 
      { 
       con.Open(); 
      } 
      catch (Exception ex) { MessageBox.Show(ex.Message); } 
      //Build DataGridView 
      try 
      { 
       sqlAdapter = new OleDbDataAdapter("SELECT SessionName, PrincipleName, SessionDate, TellerNum, Comments, SessionKey FROM [SESSION] WHERE TellerNum = @teller ORDER BY TellerNum;", con); 
       sqlCommand = new OleDbCommandBuilder(sqlAdapter); 

       sqlAdapter.InsertCommand = sqlCommand.GetInsertCommand(); 
       sqlAdapter.UpdateCommand = sqlCommand.GetUpdateCommand(); 
       sqlAdapter.DeleteCommand = sqlCommand.GetDeleteCommand(); 

       dataset = new DataSet(); 
       sqlAdapter.Fill(dataset, "[SESSION]"); 
       dataGridView1.DataSource = null; 
       dataGridView1.DataSource = dataset.Tables["[SESSION]"]; 

       for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 
        DataGridViewLinkCell linkCell = new DataGridViewLinkCell(); 
        dataGridView1[5, i] = linkCell; 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
      { 
       DataGridViewLinkCell linkCell = new DataGridViewLinkCell(); 
       dataGridView1[5, i] = linkCell; 
      } 
     } 
    } 
+0

如在搜索和筛选或搜索和选择? – Tom

+0

我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

+0

如果我使用组合框,我会让它从列表中选择一个员工(我已经完成)。然后,您将点击提交,并创建关于该员工信息的datagridview。我可以使用一个文本框,但无论哪种方式,它可能是相同的过程。我只是不知道如何让comboBox或textBox与datagridview关联。 – JoeMarvel

回答

1

您可以使用数据视图来过滤这样的数据(我已创建的数据表2列和绑定表的组合框,但数据视图上的DataGridView,那么我就可以从组合使用的ID值筛选将过滤网格的数据视图。):

private void GridFilterForm_Load(object sender, EventArgs e) 
    { 
     this.InitializeGrid(); 
    } 

    private DataView dataView; 
    private void InitializeGrid() 
    { 
     DataTable dataTable = new DataTable(); 
     dataView = new DataView(dataTable); 

     dataTable.Columns.Add("Id"); 
     dataTable.Columns.Add("Name"); 
     dataTable.Rows.Add("1", "Name1"); 
     dataTable.Rows.Add("2", "Name2"); 
     dataTable.Rows.Add("3", "Name3"); 
     dataTable.Rows.Add("4", "Name4"); 

     //Bind to dataView 
     this.dataGridView1.DataSource = dataView; 

     //make sure about the datatable to show all 
     this.comboBox1.DataSource = dataTable; 
     this.comboBox1.DisplayMember = "Name"; 
     this.comboBox1.ValueMember = "Id"; 

    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (this.comboBox1.SelectedValue !=null) 
     { 
      //Filter the DataView 
      string value= this.comboBox1.SelectedValue as string; 
      if (!string.IsNullOrEmpty(value)) 
      { 
       //filter on id 
       dataView.RowFilter = "Id = " + value; 
      } 
     } 
     else 
     { 
      dataView.RowFilter = null; 
     } 
    } 
+0

我似乎有此代码的麻烦。我无法让它产生任何东西。我添加了更多的代码,以便您可以看到我目前如何设置我的comboBox和datagridview。 – JoeMarvel

+0

在网格上设置会话表数据源的位置,您需要使用dataview。 Dataview在构造函数中使用数据表。当组合更改时,只需设置dataview rowfilter属性即可 – Avneesh

相关问题