无法启用此约束,因为并非所有值在c#中都具有相应的父值

本文关键字:约束 因为 启用 | 更新日期: 2023-09-27 17:59:13

我试图在嵌套的gridview中填充数据。收到此错误This constraint cannot be enabled as not all values have corresponding parent values。我的错误是什么?帮我解决这个问题。

这是我的代码

        validateDept.InitializeConnection();
        OleDbConnection connection = new OleDbConnection(validateDept.connetionString);
        OleDbDataAdapter AdapterCustomers = new OleDbDataAdapter(
         "SELECT EmployeeId, FirstName FROM Employee", connection);
        OleDbDataAdapter AdapterInvoices = new OleDbDataAdapter(
          "SELECT InvoiceId, InvoiceNumber, InvoiceDate, SalesPerson AS EmployeeId, Customername AS CustomerId, (Select CustomerName from Customer where Customer.CustomerId = NewInvoice_1.CustomerName) AS Customer_Name, DueDate, Tax, GrandTotal, CompanyId, InvoiceStatus FROM NewInvoice_1", connection);
        DataSet dataSet11 = new DataSet();
        //Create DataTable objects for representing database's tables
        AdapterCustomers.Fill(dataSet11, "Employee");
        AdapterInvoices.Fill(dataSet11, "Invoices");
        //Set up a master-detail relationship between the DataTables
        DataColumn keyColumn = dataSet11.Tables["Employee"].Columns["EmployeeId"];
        DataColumn foreignKeyColumn = dataSet11.Tables["Invoices"].Columns["EmployeeId"];
        dataSet11.Relations.Add("CustomerInvoice", keyColumn, foreignKeyColumn); // Getting error on this row
        //Bind the grid control to the data source
        gridControl1.DataSource = dataSet11.Tables["Employee"];
        gridControl1.ForceInitialize();
        //Assign a CardView to the relationship
        GridView cardView1 = new GridView(gridControl1);
        gridControl1.LevelTree.Nodes.Add("CustomerInvoice", cardView1);
        //Specify text to be displayed within detail tabs.
        cardView1.ViewCaption = "Invoice List of a Customer";
        //Create columns for the detail pattern View
        cardView1.PopulateColumns(dataSet11.Tables["Invoices"]);

提前谢谢。Srihari

无法启用此约束,因为并非所有值在c#中都具有相应的父值

感谢我解决的所有问题,因为在数据库中EmployeeId在Employee表中的Employe_Id在Invoice表中不匹配,所以引发了该错误。在更改/匹配两个表中的确切Id后,现在工作正常。