使用组合框获取数据以在DataGridView中显示以过滤查询结果

时间:2017-11-17 22:52:52

标签: c# winforms linq datagridview combobox

我的小组无法让这个组合框和datagridview互相交谈。应该发生的是,当您从组合框中选择一个名称时,任何附有技术ID的打开事件都应该出现。我们有过滤器工作,但我们似乎无法使两者相互交谈。这是我们到目前为止的代码:

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

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分: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();












}

答案 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();



}