【问题标题】:Getting a data to display in a DataGridView using a combobox to filter the results of the queries使用组合框在 DataGridView 中显示数据以过滤查询结果
【发布时间】:2017-11-17 22:52:52
【问题描述】:

我的小组无法让此组合框和 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();
        }
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 你必须在cmbCustomers_SelectedIndexChanged处放一些逻辑,例如,做一个接收参数(cmbCustomers SelectedValue)的方法,然后每次cmbCustomers触发事件时调用它。
  • JCM 到底是什么?我和我的两个合作伙伴已经为此工作了两个星期,但我们一直卡在 SelectedIndexChanged 事件处理程序上。

标签: c# winforms linq datagridview combobox


【解决方案1】:
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();












}

【讨论】:

  • 这将为技术 ID 提取所有已知的公开事件,并允许您从列表中选择一个。显示。希望对您有所帮助。
  • 有没有在 DataGridView 中显示相同的信息,Michael?我们希望能够更改某些列中的信息。
  • 声明要添加到网格中的变量。然后使用 GridName.Rows.Add(// variable1, variable2);按网格列的顺序放置变量。意思是,如果产品在第 0 列,那么当添加一行时,它是第一个变量。第 1 列将是第二列。等等。
  • 直接调用它而不是添加变量。 newGrid.Rows.Add(row.Cells[0].value, row.Cells[1].value);逗号分隔行的单元格。
【解决方案2】:
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();



}

【讨论】:

  • 这将添加到单独的网格视图中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 2016-04-30
  • 1970-01-01
相关资源
最近更新 更多