我的项目目的需要帮助。我正在使用gridview。其中我试图动态插入一些列。由于这些列依赖于不同的参数,因此在填充网格时将插入不同的列。为此,每当我第二次填充网格时,我将删除旧的动态添加列并添加新列。但是我正在第二次填充网格。 下面是我用来动态分配和删除列的代码。
//delete old columns
foreach (string approvalUser in tempSchemaList)
{
if (((GridViewDataColumn)dgEng.Columns[approvalUser]) != null)
dgEng.Columns.Remove((GridViewDataColumn)dgEng.Columns[approvalUser])
}
//inserting new columns
foreach (string approvalUser in approverUsers)
{
GridViewDataColumn datacolumn = new GridViewDataColumn();
datacolumn.Header = approvalUser;
datacolumn.UniqueName = approvalUser;
datacolumn.SortingState = SortingState.Ascending;
datacolumn.DataMemberBinding = new System.Windows.Data.Binding(“ab”);
Interaction.GetBehaviors(datacolumn).Add(new ClosePopupOnApplyFilterBehavior());
dgEng.Columns.Insert(insertIndex, datacolumn);
insertCount++;
insertIndex++;
}
}
在第二次填充网格时,我收到以下错误:
Line: 57
Error: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at System.Windows.ResourceManagerWrapper.GetResourceForUri(Uri xamlUri, Type componentType)
请说明错误以及该怎么做?