我有一個datagridview綁定到一個綁定源和一個窗體上的幾個按鈕。一個按鈕將一個項目添加到綁定源,另一個按鈕刪除當前選定的項目。還有一個事件處理程序,用於偵聽CurrentChanged事件並更新Remove按鈕的Enabled狀態。DataGridView綁定問題:「索引-1沒有值。」
直到我去掉datagridview中的最後一項時,所有東西都是hunky dory。然後我看到一個很醜陋的例外:
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick, Boolean clearSelection, Boolean forceCurrentCellSelection)\r\n at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean includeNewRow)
at System.Windows.Forms.DataGridView.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
at System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control value)
at System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control, Boolean originator)
at System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control value)
at System.Windows.Forms.ContainerControl.SetActiveControl(Control ctl)
at System.Windows.Forms.ContainerControl.set_ActiveControl(Control value)
at System.Windows.Forms.Control.Select(Boolean directed, Boolean forward)
at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Control.SelectNextControlInternal(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Control.SelectNextIfFocused()
at System.Windows.Forms.Control.set_Enabled(Boolean value)
at Bug3324.Form1.HandleBindingSourceCurrentChanged(Object _sender, EventArgs _e) in D:\\Dev\\TempApps\\Bug3324\\Bug3324\\Form1.cs:line 41
at System.Windows.Forms.BindingSource.OnCurrentChanged(EventArgs e)
at System.Windows.Forms.BindingSource.CurrencyManager_CurrentChanged(Object sender, EventArgs e)
at System.Windows.Forms.CurrencyManager.OnCurrentChanged(EventArgs e)
我隔離在一個小場景的問題:
private BindingSource m_bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
m_bindingSource.CurrentChanged += HandleBindingSourceCurrentChanged;
m_bindingSource.DataSource = new BindingList<StringValue>();
dataGridView1.DataSource = m_bindingSource;
btnAdd.Click += HandleAddClick;
btnRemove.Click += HandleRemoveClick;
}
private void HandleRemoveClick(object _sender, EventArgs _e)
{
m_bindingSource.RemoveCurrent();
}
private void HandleAddClick(object _sender, EventArgs _e)
{
m_bindingSource.Add(new StringValue("Some string"));
}
private void HandleBindingSourceCurrentChanged(object _sender, EventArgs _e)
{
// this line throws an exception when the last item is removed from
// the datagridview
btnRemove.Enabled = (m_bindingSource.Current != null);
}
}
public class StringValue
{
public string Value { get; set; }
public StringValue(string value)
{
Value = value;
}
}
通過純粹的實驗,我發現,如果我不改變按鈕狀態在CurrentChanged事件處理程序中,然後一切工作正常。所以我懷疑某種操作順序問題。但是什麼?爲什麼試圖與datagridview完全無關的更改會導致問題?
爲了使事情更加有趣,如果程序在附帶調試器的情況下在VS中啓動,則通常無害(或根本不顯示)。但如果它自己執行,則會彈出一個消息框,其中包含異常詳細信息。
我試着處理datagridview上的RowEnter事件,發現在這種情況下,它仍然認爲它有一行並試圖從綁定源中檢索Current項,但m_bindingSource.Current
已經爲null。爲什麼在處理CurrentChanged事件時這只是一個問題?
任何和所有的幫助將不勝感激。謝謝。
您是否真的驗證過它是Button.Enabled和_not_讀取BindSource.Current是至關重要的? – 2010-09-10 19:53:26
@亨克:看起來如此。我將Enabled設置代碼分成兩行:「var currentIsNotNull = m_bindingSource.Current!= null; btnRemove.Enabled = currentIsNotNull;」。然後由btnRemove.Enabled設置器拋出異常。也就是說,如果我根本沒有將Enabled屬性的值設置在綁定源上,那麼一切運行良好,所以也許它是read和Enabled setter的組合。 – 2010-09-10 20:07:37
我試過你的代碼,它看起來很完美。沒有問題,直接從Visual Studio調試器和.exe沒有例外。 ... – pdiddy 2010-09-10 20:22:43