我正在努力找出 UltraGrid Bands 机制的奇怪行为。以下代码包含重新创建问题所需的一切。
我正在创建一个数据集并用两个数据表填充它。然后我将 DataSet 分配为 BindingSource 对象的 DataSource。该 BindingSource 对象设置为 UltraGrid.DataSource。
下面的代码将有助于完全理解启动单个 PrepareData() 方法后发生的情况。网格显示 3 个记录,第一个可展开以显示 DataRelation 引用的第二个 Band。您可以通过注释掉第二个电话来检查。
public partial class Form1 : Form {
BindingSource bs = new BindingSource();
DataSet ds = new DataSet();
public Form1() {
InitializeComponent();
DoLayout();
Bind();
PrepareData();
PrepareData();
}
private void DoLayout() {
ultraGrid1.DisplayLayout.Override.ExpansionIndicator = Infragistics.Win.UltraWinGrid.ShowExpansionIndicator.CheckOnDisplay;
}
private void Bind() {
bs.DataSource = ds;
ultraGrid1.DataSource = bs;
}
private void PrepareData() {
// you need to keep watching the ultraGrid1.DisplayLayout.Bands.All property
// all the time - normally it contains one entry,
// the default {NewDataSet} entry
bs.SuspendBinding();
ds.Relations.Clear();
ds.Clear();
ds.Tables.Clear();
// when the above code will run for the second time, you can see that
// Bands.All property will claim to have the standard {NewDataSet} entry
// again. Seems like everything is working well
DataTable dt = new DataTable("TABLE1");
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Rows.Add("1", "1", "1");
dt.Rows.Add("2", "2", "2");
dt.Rows.Add("3", "3", "3");
// after adding the datatable to the dataset, we can see that there was
// a change made to the DisplayLayout.Band.All list of the ultraGrid1
// {NewDataSet} will change to {TABLE1}
//
// now watch the behavior when the method is run for the second time
ds.Tables.Add(dt);
// after the second run, you can see the first problem in the Bands.All entries
dt = new DataTable("TABLE2");
dt.Columns.Add("scol1");
dt.Columns.Add("scol2");
dt.Rows.Add("s1", "1");
dt.Rows.Add("s2", "1");
dt.Rows.Add("s3", "1");
// the Bands.All property still will say there is only one element in it
// but not anymore once the code is executed for the second time
ds.Tables.Add(dt); // for the second code run - here is the second problem
// now the first time we add that relation, you can see that
// a new entry exists in the Bands.All property of the ultraGrid1: {T1T2}
dt.ParentRelations.Add("T1T2", ds.Tables["TABLE1"].Columns["col1"], dt.Columns["scol2"], false);
// after the second run of the code, here you can see the third problem
bs.ResumeBinding();
}
}
现在,如果您再次运行 PrepareData() 方法,网格就会变得混乱。我实际上找到了导致问题的原因,因此上面代码中的大量注释,但我无法弄清楚为什么会发生这种情况。因此,无论调用多少次方法,我都希望网格的行为完全相同。
有谁知道这可能是什么原因?
我已经尝试清空数据源,并重新分配它们;厌倦了改变调用方法的顺序;甚至尝试调用 BandsCollection 对象的非公共方法,如“ClearAllButBandZero”和“Clear”,但都无济于事。
我在 Infragistics DevCenter 的知识库中找到了一篇文章: http ://devcenter.infragistics.com/Support/KnowledgeBaseArticle.Aspx?ArticleID=1751 但这对我没有帮助。每次重新构建 DataSet 时,UltraGrid.DisplayLayout.Bands 集合都会变得越来越乱。