2014-06-24 56 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

相關問題