vb.net.如何将数据集绑定到 DataRepeater? [英] vb.net. How do I bind dataset to DataRepeater?

查看:20
本文介绍了vb.net.如何将数据集绑定到 DataRepeater?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何将数据集/数据表绑定到数据转发器并将数据元素绑定到数据集/数据表的列的 vb.net 示例?谢谢

I am looking for a vb.net example of how to bind a dataset/datatable to data repeater and have the data elements bound to the columns of the dataset/datatable? Thanks

推荐答案

起初我以为你想要一个网络转发器,但从你的评论中我意识到你的意思是 Microsoft.VisualBasic.PowerPacks.DataRepeater.

At first I thought you wanted a web repeater, but from your comments I realized you meant Microsoft.VisualBasic.PowerPacks.DataRepeater.

我需要您提供更多信息以提供最有用的示例代码(见下文).

I need some more info from you to give the most helpful sample code (see below).

使用 DataRepeater 的基本步骤是:
1) 安装 Visual Basic Power Packs 3 链接
2) 打开一个 VB.net Winforms 项目并将 DataRepeater 拖到您的表单中
3) 通过 Add->New Item 菜单向您的项目添加一个新的数据集
4) 在设计窗口中,根据需要设置列
5) 从数据->显示数据源菜单
打开数据源窗口6) 在表单处于设计模式时,转到数据源"窗口中的数据集",然后使用表名旁边的下拉框选择详细信息"
7) 将表格拖到 DataRepeater 控件的顶部(在您的表单上).表格字段现在应该列在您的 DataRepeater 上.您可以移动它们.
8) 在运行时,您可以加载数据集,数据中继器会自动反映数据的变化.不需要.Databind
例如

The basic steps of using a DataRepeater are:
1) Install the Visual Basic Power Packs 3 Link
2) Open a VB.net Winforms project and drag a DataRepeater to your form
3) Add a new Dataset to your project via Add->New Item menu
4) In the design window, set up columns as desired
5) Open the Data Sources window from Data->ShowDataSources menu
6) With your form in design mode, go to the Dataset in the Data Sources window and use the dropdown box next to the table name to select "Details"
7) Drag the table to top section of the DataRepeater control (on your form). The table fields should now be listed on your DataRepeater. You can move them around.
8) At runtime, you can load the Datset and the DataRepeater will automatically reflect the data changes. No .Databind is required
ex.

me.DataSet1.Tables(0).Columns.Add(New String() {"John", "Doe", "Accountant"}  

myDataAdapter.Fill(me.DataSet1.Tables(0)) 

您是否被这些步骤中的任何一个绊倒了?

Do you get tripped up on any of those steps?

据我所知,DataRepeater 似乎是在设计时将数据表添加/映射到 DataRepeater 的情况下使用的.然后您所要做的就是在运行时填充数据表,DataReader 会自动显示数据.

From what I have seen, it looks like DataRepeater is meant to be used in situations were you add/map the datatable to the DataRepeater at design-time. Then all you have to do is fill the datatable at run-time and the DataReader shows the data automatically.

如果您真的想在运行时将表/控件添加到 DataRepeater,这里是我为您编写的示例.它假设有一个名为 Form3 的表单,有一个名为 DataRepeater1 的 DataRepeater 和一个名为 Button1 的按钮...

If you really want to add the table/controls to the DataRepeater at run time, here's an example I coded for you. It assumes a form named Form3 with a DataRepeater named DataRepeater1 and a button named Button1...

Public Class Form3

    ''Set up demo DataSet/DataTable
    Const FRUIT_COL As String = "Fruit"
    Const COLOR_COL As String = "Color"
    MyDataSet = New DataSet
    MyDataSet.Tables.Add("MyTable")
    With MyDataSet.Tables(0)
        .Columns.Add(FRUIT_COL, GetType(System.String))
        .Columns.Add(COLOR_COL, GetType(System.String))
    End With

    ''Populate the DataTable with sample data. You would be loading from SQL
    With MyDataSet.Tables(0)
        .Rows.Add(New String() {"Apple", "Red"})
        .Rows.Add(New String() {"Orange", "Orange"})
        .Rows.Add(New String() {"Banana", "Yellow"})
    End With

    ''These objects would normally be created automatically if you added DataTable to DataRepeater at design-time
    FruitLabel = New Label
    FruitTextBox = New TextBox
    ColorLabel = New Label
    ColorTextBox = New TextBox
    With FruitLabel
        .AutoSize = True
        .Location = New Point(10, 20)
        .Name = "FruitLabel"
        .Text = FRUIT_COL
    End With
    With ColorLabel
        .AutoSize = True
        .Location = New Point(10, 60)
        .Name = "FruitLabel"
        .Text = FRUIT_COL
    End With
    With FruitTextBox
        .Location = New Point(50, 20)
        .Size = New Size(60, 15)
        .DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyDataSet.Tables(0), FRUIT_COL, True))
    End With
    With ColorTextBox
        .Size = New Size(60, 15)
        .Location = New Point(50, 60)
        .DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyDataSet.Tables(0), COLOR_COL, True))
    End With

    ''Add the controls that will be displayed for each row in DataTable
    With DataRepeater1
        .ItemTemplate.Controls.Add(FruitLabel)
        .ItemTemplate.Controls.Add(FruitTextBox)
        .ItemTemplate.Controls.Add(ColorLabel)
        .ItemTemplate.Controls.Add(ColorTextBox)
    End With

    ''Run-time population of DataRepeater from your DataTable
    DataRepeater1.DataSource = MyDataSet
    DataRepeater1.DataMember = MyDataSet.Tables(0).TableName

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Example of how you can add additional rows with form elements
    With MyDataSet.Tables(0)
        .Rows.Add(New String() {"Grapes", "Green"})
    End With
End Sub
End Class

既然你已经看到了代码,我希望你不要使用它:)
我建议您在设计时设置您的 DataSet 结构或使用不同的控件来显示您的数据.

Now that you have seen the code, I hope you don't use it :)
I suggest you either set up your DataSet structure at design-time or use a different control to display your data.

这是一个链接,其中包含有关使用 DataRepeater 的典型方法的大量信息:链接

Here's a link that has lots of information about the typical way to use DataRepeater: Link

最终编辑

用户的最后一个错误是区分大小写的问题.以下代码行中的字符串Text"必须大写以匹配控件的属性名称.我建议为测试"创建一个常量以避免出现这种错字.

The user's last bug was a case-sensitivity issue. The string "Text" in the following line of code must be capitalized to match the control's property name. I recommend creating a const for "Test" to avoid this typo.

[ControlName].DataBindings.Add(New System.Windows.Forms.Binding("Text", [DataTable], [Column Name], True))

这篇关于vb.net.如何将数据集绑定到 DataRepeater?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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