如何将Combobox绑定到DataGridView

时间:2017-11-16 20:46:57

标签: c# datagridview combobox

在下面的代码中,我使用了LINQ方法来处理我的问题 数据集并成功显示组合框中的名称列表并在GridView中显示数据表。我遇到的问题是让组合框和gridview相互连接,这样当在组合框中选择客户时,他们的记录将显示在下面的Gridview中。

-Swift.h

以下是用于连接Customers和Incidents表并将数据集链接到组合框的LINQ查询

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Incident_Maintenance_Form
{
    public partial class frmIncidentMaintenance : Form
    {
        public frmIncidentMaintenance()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            TechSupportEntities techSupport = new TechSupportEntities();

下面是使用的LINQ查询,它连接Customers和Incidents表并将数据源链接到datagridview

                var customers = (from customer in techSupport.Customers
                                 join incidents in techSupport.Incidents
                                 on customer.CustomerID equals 
                                 incidents.CustomerID
                                 orderby customer.Name
                                 where incidents.TechID != null
                                 where incidents.DateOpened != null
                                select new { customer.CustomerID, 
                                customer.Name }).Distinct();
            cmbCustomers.DataSource = customers.ToList();
            cmbCustomers.DisplayMember = "Name";
            cmbCustomers.ValueMember = "CustomerID";

以下是我尝试将组合框链接到datagridview的代码。

                var products = from customer in techSupport.Customers
                               from incident in customer.Incidents

                               select new
                               {
                                   incident.ProductCode,
                                   incident.TechID,
                                   incident.Title,
                                   incident.DateOpened,
                                   incident.DateClosed,
                                   incident.Description
                               };


                dataGridView1.DataSource = products.ToList();
            //    customersBindingSource.DataSource = customers.ToList();


            // TODO: This line of code loads data into the 'techSupportDataSet.Customers' table. You can move, or remove it, as needed.
            //  this.customersTableAdapter.Fill(this.techSupportDataSet.Customers);

        }

0 个答案:

没有答案