簡體   English   中英

更新添加到未綁定WinForms DataGridView的行會自動失敗-為什么?

[英]Updating rows added to an unbound a WinForms DataGridView fails silently - why?

“我的主/詳細信息”表單使用未綁定的DataGridView列出與過濾器“主”部分匹配的客戶)在DataGridView中選擇一行(RowEnter)將在值中填充“詳細信息”部分。 我有一個正常工作的“保存”按鈕,用於保存表單,重新綁定數據網格,然后重置表單。 問題是當詳細信息更改時更新DataGridView,現在bind方法達到dataGridView1.Rows.Clear()為止,然后退出但沒有錯誤。

我需要保存表單並更新網格,還需要了解為什么當前無法使用。

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCell dgc = dataGridView1["ID", int.Parse(e.RowIndex.ToString())];
        Guid customerID = Guid.Parse(dgc.Value.ToString());

        if (IsPageValid() && dataGridView1.Focused)
        {
            if (KeepChanges())
            {
                SaveForm();
                BindDataGrid(string.Empty);
            }
        }            

        Customer = _customerRepo.GetByID(customerID);
        BindCustomerDetails(Customer);
    }

想法是,加載表格並選擇一個客戶,更改表格上的一些詳細信息,然后選擇一個新客戶(在網格中選擇另一行),並且有未提交的更改。 IsPageValid檢查表單上的詳細信息是否會創建有效的客戶。 KeepChanges從MessageBox返回YesNo(“此記錄有更改。是否要保留它們?”)如果用戶單擊Yes,則調用SaveForm()以保留詳細信息。 然后調用BindDataGrid(string.Empty)。 到目前為止,一切都很好,在Form_Load()上調用BindDataGrid(string.Empty),但是這一次當行dataGridView1.Rows.Clear()命中時,調試器停止並且控制權返回到程序,所有該方法中超出該點的代碼已執行,我無法弄清原因。

public void BindDataGrid(String filterText)
    {
        var customers = String.IsNullOrEmpty(filterText) ? _customerRepo.GetAll() : _customerRepo.GetFiltered(filterText);

        dataGridView1.Rows.Clear();
        dataGridView1.ColumnCount = 5;
        dataGridView1.Columns[0].Name = "ID";
        dataGridView1.Columns[0].Visible = false;
        dataGridView1.Columns[1].Name = "Name";
        dataGridView1.Columns[2].Name = "Address";
        dataGridView1.Columns[2].FillWeight = 300;
        dataGridView1.Columns[3].Name = "Phone";
        dataGridView1.Columns[3].FillWeight = 80;
        dataGridView1.Columns[4].Name = "Mobile";
        dataGridView1.Columns[4].FillWeight = 80;

        foreach (var customer in customers)
        {
            String address = String.IsNullOrEmpty(customer.Address.Street) ? "no address" : String.Format("{0}, {1}, {2}, {3}", customer.Address.Street, customer.Address.Town, customer.Address.County, customer.Address.Postcode);
            dataGridView1.Rows.Add(customer.ID, customer.ToString(), address, customer.Address.Phone, customer.Mobile);
        }
    }

我嘗試將其更改為使用DataTable,將DataBinding設置為null並使用DataBinding對象,但是它們似乎都遇到了相同的問題。 DataGridView似乎沒有更新或不允許更新。 就designer.cs文件而言,據我所知,似乎沒有阻止我的任何事情:

// dataGridView1
        // 
        this.dataGridView1.AllowUserToAddRows = false;
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(4, 4);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.ReadOnly = true;
        this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.dataGridView1.Size = new System.Drawing.Size(736, 330);
        this.dataGridView1.TabIndex = 5;
        this.dataGridView1.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_RowEnter);

問題是您試圖從行事件(RowEnter)中清除行。 這幾乎肯定會引發您需要捕獲的異常。

根本問題是數據網格的重新綁定。 我很確定您不必在每次保存數據時都這樣做。 您最需要做的是保存詳細信息部分中特定客戶的內容。

如果基礎數據源支持IBindingList,那么您要做的就是從網格中獲取客戶記錄,並使用表單中的值對其進行更新。 數據綁定應注意更新網格內容。

有關更多信息,請參見DataSource屬性上的MSDN文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM