2017-11-17 165 views
0

我的組無法讓此組合框和datagridview相互交談。應該發生的事情是,當你從組合框中選擇一個名字時,任何帶有技術ID的開放事件都應該出現。我們已經有了過濾器的工作,但我們似乎無法讓兩者相互交談。這裏是我們迄今的代碼:使用組合框來獲取要在DataGridView中顯示的數據以過濾查詢結果

public partial class frmIncidentMaintenance : Form 
{ 
    public Incident incident; 
    public frmIncidentMaintenance() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

     TechSupportEntities techSupport = new TechSupportEntities(); 

      var customers = (from customer in techSupport.Customers 
          orderby customer.Name 
          select new { customer.CustomerID, customer.Name 
    }).Distinct(); 
     cmbCustomersBindingSource.DataSource = customers.ToList(); 
     cmbCustomersBindingSource.DisplayMember = "Name"; 
     cmbCustomersBindingSource.ValueMember = "CustomerID"; 





     var products = from customer in techSupport.Customers 
         from incident in customer.Incidents 
         where incident.TechID != null 
         where incident.DateClosed == null 
         select new 
         { 
          incident.ProductCode, 
          incident.TechID, 
          incident.Title, 
          incident.DateOpened, 
          incident.DateClosed, 
          incident.Description 
         }; 


     dataGridView1.DataSource = products.ToList(); 


    } 

    private void cmbCustomers_SelectedIndexChanged(object sender, EventArgs 
e) 
    {     




    } 

    private void dataGridView_CellContentClick(object sender, 
DataGridViewCellEventArgs e) 
    { 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
     } 
    } 
} 

任何幫助,將不勝感激。

+0

你必須把一些邏輯在cmbCustomers_SelectedIndexChanged,例如,使接收的參數(cmbCustomers的SelectedValue)的方法,然後調用它每次cmbCustomers觸發事件的時間。 – JCM

+0

像什麼JCM?我和我的兩個合作伙伴已經爲此工作了兩週,並且我們一直在SelectedIndexChanged事件處理程序中停滯不前。 – Venomsamurai

回答

0
private void cmbCustomers_SelectedIndexChanged(object sender, EventArgs 
e) 
{   
    //This is the string for your tech id 
    string tech_id = combobox.SelectedItem.ToString(); 

    //Searches the datagridview 
    int rowIndex =0; 
    foreach(DataGridViewRow row in [name of grid here]) 
    { 
     //matches tech-id to gridrow value 
     if(tech_id == row.Cells[//Cell for tech_id].Value.ToString()) 
     { 
      //Looks for open incidents (ill guess a numberical value 1 or 0 
      int open_close_value = Convert.ToInt16(row.Cells[openvalue]contains this data].value.ToString()); 
      if(open_close_value == value your looking for) 
      { 
       //rowindex will give you the row of the gridview 
       rowindex = row.Index; 
       string incident = row.Cells[you incident].Value.ToString(); 

       // for pushing data to be displayed in textboxes make it 
       // easier on yourself and allow the search engine to pull 
       //all incidents that are open with that tech_id add them to a list 
       //this will add the row number plus the incident 
       Listbox1.Items.Add(rowindex+","+incident); 

       //now on listbox1 selectedindex change all you do is parse rowindex, and use that rownumber to pull data out of database. 
       // your parse string should look like this. 
       //string[] data = listbox1.Selectedindex.ToString().Split(new string[] {","}, StringSplitOptions.None); 
       //to get data from gridview 
       //string incident= yourGrid.Rows[data[0]].Cell[the cell you want].value.ToString(); 












} 
+0

這將拉開所有已知開放事件的技術ID,並允許您從列表中選擇一個。顯示。希望它能幫助你。 –

+0

有沒有辦法在DataGridView中顯示相同的信息,邁克爾?我們希望能夠在某些欄目中更改信息。 – Venomsamurai

+0

聲明要添加到網格中的變量。然後使用GridName.Rows.Add(// variable1,variable2);按照網格的列順序放置您的變量。意思是,如果產品在列0中,那麼當添加一行時,它是第一個變量。第1列將是第二列。等等。 –

0
private void cmbCustomers_SelectedIndexChanged(object sender, EventArgs e) 
{   
//This is the string for your tech id 
string tech_id = combobox.SelectedItem.ToString(); 

//Searches the datagridview 

foreach(DataGridViewRow row in [name of grid here]) 
{ 
    //matches tech-id to gridrow value 
    if(tech_id == row.Cells[//Cell for tech_id].Value.ToString()) 
    { 
     //Looks for open incidents (ill guess a numberical value 1 or 0 
     int open_close_value = Convert.ToInt16(row.Cells[openvalue]contains this data].value.ToString()); 
     if(open_close_value == value your looking for) 
     { 
      //rowindex will give you the row of the gridview 
      rowindex = row.Index; 
      string incident = row.Cells[you incident].Value.ToString(); 

      // for pushing data to be displayed in textboxes make it 
      // easier on yourself and allow the search engine to pull 
      //all incidents that are open with that tech_id add them to a grid 
      //this will add the row number plus the incident 
      NewGrid.Rows.Add(row.Cells[0].Value, row.Cells[1].Value,row.Cells[2].Value, row.Cells[3].Value,row.Cells[4].Value,row.Cells[5].value); 

      //now on listbox1 selectedindex change all you do is parse rowindex, and use that rownumber to pull data out of database. 
      // your parse string should look like this. 
      //string[] data = listbox1.Selectedindex.ToString().Split(new string[] {","}, StringSplitOptions.None); 
      //to get data from gridview 
      //string incident= yourGrid.Rows[data[0]].Cell[the cell you want].value.ToString(); 



} 
+0

這將添加到一個單獨的gridview –

相關問題