Datarepeater和tablelayoutpanel:重复rowstyle的异常 [英] Datarepeater and tablelayoutpanel: exception with duplicate rowstyle

查看:81
本文介绍了Datarepeater和tablelayoutpanel:重复rowstyle的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验Microsoft.VisualBasic.PowerPacks包中的DataRepeater控件,希望它能帮助我以更有效的方式在我的项目中显示数据。当从设计器给出ItemTemplate时,它工作得很好,但是对于我的项目,我需要控件根据情况采用不同的模板:因此,我尝试编写一些代码来在运行时更改模板。但是,在尝试向控件提供DataSource后,我得到一个System.ArgumentException异常,并显示以下消息:无法在多个位置添加或插入项RowStyle。您必须先将其从当前位置删除或克隆它。



这是我正在使用的代码,其中DR1是DataRepeater和CNT_和TB_论据是我用它来保持名称的轨道常量:

I've been experimenting with the DataRepeater control from the Microsoft.VisualBasic.PowerPacks package, hoping it will help me display data in my project in a more efficient manner. It works pretty well when the ItemTemplate is given from the designer, but for my project I'll need the control to take different templates depending on the situation: as such, I've tried writing some code to change the template at runtime. When doing so, however, I get a System.ArgumentException exception after trying to give a DataSource to the control, with the following message:Cannot add or insert the item 'RowStyle' in more than one place. You must first remove it from its current location or clone it.

This is the code I'm using, with DR1 being the DataRepeater and the CNT_ and TB_ arguments being constants I use to keep track of the names:

DR1.BeginResetItemTemplate()
Dim tlp As New TableLayoutPanel With {
    .Dock = DockStyle.Fill,
    .RowCount = 2,
    .ColumnCount = 2
}
tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 75))
tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 25))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 25))

Dim lblName As New Label With {
    .Dock = DockStyle.Fill,
    .Text = "Name:",
    .TextAlign = ContentAlignment.MiddleLeft
}
Dim tbxName As New TextBox With {
    .Dock = DockStyle.Fill,
    .Name = CNT_NAME
}
Dim lblBday As New Label With {
    .Dock = DockStyle.Fill,
    .Text = "Birth date:",
    .TextAlign = ContentAlignment.MiddleLeft
}
Dim dtpBday As New DateTimePicker With {
    .Dock = DockStyle.Fill,
    .Name = CNT_BDAY,
    .Format = DateTimePickerFormat.Short
}

tlp.Controls.Add(lblName, 0, 0)
tlp.Controls.Add(tbxName, 1, 0)
tlp.Controls.Add(lblBday, 0, 1)
tlp.Controls.Add(dtpBday, 1, 1)

DR1.ItemTemplate.Controls.Clear()
DR1.ItemTemplate.Controls.Add(tlp)
DR1.EndResetItemTemplate()

Dim dt As New DataTable
dt.Columns.Add(TB_NAME)
dt.Columns.Add(TB_BDAY)

dt.Rows.Add("Name 1", New Date(1991, 11, 8))
dt.Rows.Add("Name 2", New Date(1992, 8, 16))
dt.Rows.Add("Name 3", New Date(1993, 7, 5))

DR1.DataSource = dt





我的尝试:



我相信这个问题是由我用来定位控件的TableLayoutPanel引起的:试图添加一个TableLay由设计者输出到ItemTemplate的outPanel引起了同样的异常。但是,我必须在我的项目中使用的所有自定义模板也使用这种类型的Panel,我不能只是更改结构以避免错误。我尝试在线查找这个bug,但是在DataRepeater上查找资源非常困难,特别是对于.NET:我发现此问题的唯一其他人最终不得不放弃控制权。有谁知道解决这个问题的方法?在此先感谢。



What I have tried:

I believe this issue is caused by the TableLayoutPanel I'm using to position the controls: trying to add a TableLayoutPanel to the ItemTemplate by designer caused the same exception. All the custom templates I have to use in my project, however, also use that type of Panel, and I can't just change the structure to avoid the bug. I tried looking up this bug online, but finding resources on the DataRepeater is quite difficult, especially for .NET: the only other person I found with this problem had to give up on the control in the end. Does anyone know a way to resolve this issue? Thanks in advance.

推荐答案

最后我设法找到了自己的解决方案,我会把它留在这里以防其他人需要它。我已经将模板创建移动到单独的函数CreateTemplate()并为事件ItemCloning添加了一个新的处理程序:这样,当一个新项目被添加到转发器时,该函数将创建一个全新的模板。新代码如下所示:

In the end I've managed to find a solution on my own, I'll leave it here in case anyone else needs it. I've moved the template creation to a separate function CreateTemplate() and added a new handler for the event ItemCloning: this way, when a new item would be added to the repeater, the function will create an entirely new template instead. The new code looks like this:
Protected Overrides Sub OnLoad(e As EventArgs)
    MyBase.OnLoad(e)

    DR1.BeginResetItemTemplate()
    Dim tlp = CreateTemplate()
    DR1.ItemTemplate.Controls.Clear()
    DR1.ItemTemplate.Controls.Add(tlp)
    DR1.EndResetItemTemplate()

End Sub

Private Sub ItemCloning(sender As Object, e As DataRepeaterItemCloneEventArgs) Handles DR1.ItemCloning

    Dim origin As DataRepeaterItem = e.Source

    If TypeOf origin.Controls(0) Is TableLayoutPanel Then
        e.Target = New DataRepeaterItem
        e.Target.Controls.Add(CreateTemplate)
        e.Handled = True
    End If

End Sub

这足以完全消除问题。希望这有帮助!

This was enough to completely eliminate the problem. Hope this helps!


这篇关于Datarepeater和tablelayoutpanel:重复rowstyle的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆