dgv selectionchanged c#winforms

时间:2016-05-29 16:32:53

标签: c# winforms datagridview selectionchanged

我面临一个非常奇怪的问题,应该非常简单。 我想要实现的目标:我有2个datagrids:1个带有invoiceheaders,1个带有invoiceDetails。

当我点击发票抬头中的特定行时,invoicedetails需要更改并显示该发票的特定详细信息。我正在使用的事件是dgvInvoiceHeaders_SelectionChanged

视图中的代码:

 private void InvoiceListView_Load(object sender, EventArgs e)
    {

        int invoiceId = 1;


            invoiceHeadersBinding.DataSource = invoiceListPresenter.getInvoiceHeaders();
            dgvInvoiceHeaders.DataSource = invoiceHeadersBinding;
            setInvoiceHeaderColumns();

            if (dgvInvoiceHeaders.CurrentRow != null)
            {
                Int32.TryParse(dgvInvoiceHeaders.CurrentRow.Cells[0].FormattedValue.ToString(), out invoiceId);
            }

            dgvInvoiceDetails.DataSource = invoiceListPresenter.getSelectedInvoiceDetails(invoiceId);        
            dgvInvoiceDetails.DataSource = invoiceListPresenter.invoiceDetails;
            setInvoiceDetailColumns();




    }
  private void dgvInvoiceHeaders_SelectionChanged(object sender, EventArgs e)
    {


        dgvInvoiceDetails.DataSource = invoiceListPresenter.getSelectedInvoiceDetails(Convert.ToInt32(dgvInvoiceHeaders.CurrentRow.Cells[0].FormattedValue.ToString()));

        dgvInvoiceDetails.DataSource = invoiceListPresenter.invoiceDetails;
        setInvoiceDetailColumns();

    }

这是我调用的方法的代码:

 public List<tbl_invoices> invoiceHeaders;
   public BindingList<tbl_invoices> getInvoiceHeaders()
   {
       try
       {
           using (var invoices = new DBCrownfishEntities())
           {
               invoices.Configuration.LazyLoadingEnabled = false;
               var invoice = from i in invoices.tbl_invoices
                             select i;

               invoiceHeaders = invoice.ToList();
               var listBinding = new BindingList<tbl_invoices>(invoiceHeaders);
                return listBinding;
           }

       }
       catch (Exception exe)
       {

           throw exe;
       }
   }
  public List<tbl_invoiceDetail> invoiceDetails;
   public BindingList<tbl_invoiceDetail> getSelectedInvoiceDetails(int invoiceID)
   {
       try
       {
           using (var invoices = new DBCrownfishEntities())
           {
               invoices.Configuration.LazyLoadingEnabled = false;
               var invoiceDetail = from i in invoices.tbl_invoiceDetail
                                   where i.InvoiceID == invoiceID
                                   select i;

               invoiceDetails = invoiceDetail.ToList();
               var listBinding = new BindingList<tbl_invoiceDetail>(invoiceDetails);

               return listBinding;
           }
       }
       catch (Exception exe)
       {

           throw exe;
       }

   }

但是使用此示例正确加载了formload。但是在事件被触发后,我的invoiceheader datagrid为空。当我将文本放在方法dgvInvoiceHeaders_selectionChanged()的注释中时,我看到两个数据网格都被正确填充。

向正确的方向推进将非常友好。

0 个答案:

没有答案